|
|
@ -17,7 +17,7 @@ |
|
|
|
package org.apache.dolphinscheduler.service.quartz; |
|
|
|
package org.apache.dolphinscheduler.service.quartz; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource; |
|
|
|
import com.alibaba.druid.pool.DruidDataSource; |
|
|
|
import org.quartz.SchedulerException; |
|
|
|
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
|
|
import org.quartz.utils.ConnectionProvider; |
|
|
|
import org.quartz.utils.ConnectionProvider; |
|
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.Connection; |
|
|
@ -28,196 +28,24 @@ import java.sql.SQLException; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class DruidConnectionProvider implements ConnectionProvider { |
|
|
|
public class DruidConnectionProvider implements ConnectionProvider { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
private final DruidDataSource dataSource; |
|
|
|
* JDBC driver |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String driver; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* JDBC URL |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String URL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Database user name |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String user; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Database password |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String password; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Maximum number of database connections |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public int maxConnections; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The query that validates the database connection |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String validationQuery; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Whether the database sql query to validate connections should be executed every time |
|
|
|
|
|
|
|
* a connection is retrieved from the pool to ensure that it is still valid. If false, |
|
|
|
|
|
|
|
* then validation will occur on check-in. Default is false. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private boolean validateOnCheckout; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The number of seconds between tests of idle connections - only enabled |
|
|
|
|
|
|
|
* if the validation query property is set. Default is 50 seconds. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private int idleConnectionValidationSeconds; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The maximum number of prepared statements that will be cached per connection in the pool. |
|
|
|
|
|
|
|
* Depending upon your JDBC Driver this may significantly help performance, or may slightly |
|
|
|
|
|
|
|
* hinder performance. |
|
|
|
|
|
|
|
* Default is 120, as Quartz uses over 100 unique statements. 0 disables the feature. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public String maxCachedStatementsPerConnection; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Discard connections after they have been idle this many seconds. 0 disables the feature. Default is 0. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private String discardIdleConnectionsSeconds; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Default maximum number of database connections in the pool. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static final int DEFAULT_DB_MAX_CONNECTIONS = 10; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
public DruidConnectionProvider(){ |
|
|
|
* The maximum number of prepared statements that will be cached per connection in the pool. |
|
|
|
this.dataSource = SpringApplicationContext.getBean(DruidDataSource.class); |
|
|
|
*/ |
|
|
|
} |
|
|
|
public static final int DEFAULT_DB_MAX_CACHED_STATEMENTS_PER_CONNECTION = 120; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Druid connection pool |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private DruidDataSource datasource; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* get connection |
|
|
|
|
|
|
|
* @return Connection |
|
|
|
|
|
|
|
* @throws SQLException sql exception |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Connection getConnection() throws SQLException { |
|
|
|
public Connection getConnection() throws SQLException { |
|
|
|
return datasource.getConnection(); |
|
|
|
return dataSource.getConnection(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* shutdown data source |
|
|
|
|
|
|
|
* @throws SQLException sql exception |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void shutdown() throws SQLException { |
|
|
|
public void shutdown() throws SQLException { |
|
|
|
datasource.close(); |
|
|
|
dataSource.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* data source initialize |
|
|
|
|
|
|
|
* @throws SQLException sql exception |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void initialize() throws SQLException { |
|
|
|
public void initialize() throws SQLException { |
|
|
|
if (this.URL == null) { |
|
|
|
//NOP
|
|
|
|
throw new SQLException("DBPool could not be created: DB URL cannot be null"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this.driver == null) { |
|
|
|
|
|
|
|
throw new SQLException("DBPool driver could not be created: DB driver class name cannot be null!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this.maxConnections < 0) { |
|
|
|
|
|
|
|
throw new SQLException("DBPool maxConnectins could not be created: Max connections must be greater than zero!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
datasource = new DruidDataSource(); |
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
datasource.setDriverClassName(this.driver); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
throw new SchedulerException("Problem setting driver class name on datasource", e); |
|
|
|
|
|
|
|
} catch (SchedulerException e1) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
datasource.setUrl(this.URL); |
|
|
|
|
|
|
|
datasource.setUsername(this.user); |
|
|
|
|
|
|
|
datasource.setPassword(this.password); |
|
|
|
|
|
|
|
datasource.setMaxActive(this.maxConnections); |
|
|
|
|
|
|
|
datasource.setMinIdle(1); |
|
|
|
|
|
|
|
datasource.setMaxWait(0); |
|
|
|
|
|
|
|
datasource.setMaxPoolPreparedStatementPerConnectionSize(DEFAULT_DB_MAX_CONNECTIONS); |
|
|
|
|
|
|
|
if (this.validationQuery != null) { |
|
|
|
|
|
|
|
datasource.setValidationQuery(this.validationQuery); |
|
|
|
|
|
|
|
if(!this.validateOnCheckout){ |
|
|
|
|
|
|
|
datasource.setTestOnReturn(true); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
datasource.setTestOnBorrow(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
datasource.setValidationQueryTimeout(this.idleConnectionValidationSeconds); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getDriver() { |
|
|
|
|
|
|
|
return driver; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setDriver(String driver) { |
|
|
|
|
|
|
|
this.driver = driver; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public String getURL() { |
|
|
|
|
|
|
|
return URL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setURL(String URL) { |
|
|
|
|
|
|
|
this.URL = URL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public String getUser() { |
|
|
|
|
|
|
|
return user; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setUser(String user) { |
|
|
|
|
|
|
|
this.user = user; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public String getPassword() { |
|
|
|
|
|
|
|
return password; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setPassword(String password) { |
|
|
|
|
|
|
|
this.password = password; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public int getMaxConnections() { |
|
|
|
|
|
|
|
return maxConnections; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setMaxConnections(int maxConnections) { |
|
|
|
|
|
|
|
this.maxConnections = maxConnections; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public String getValidationQuery() { |
|
|
|
|
|
|
|
return validationQuery; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setValidationQuery(String validationQuery) { |
|
|
|
|
|
|
|
this.validationQuery = validationQuery; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public boolean isValidateOnCheckout() { |
|
|
|
|
|
|
|
return validateOnCheckout; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setValidateOnCheckout(boolean validateOnCheckout) { |
|
|
|
|
|
|
|
this.validateOnCheckout = validateOnCheckout; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public int getIdleConnectionValidationSeconds() { |
|
|
|
|
|
|
|
return idleConnectionValidationSeconds; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setIdleConnectionValidationSeconds(int idleConnectionValidationSeconds) { |
|
|
|
|
|
|
|
this.idleConnectionValidationSeconds = idleConnectionValidationSeconds; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public DruidDataSource getDatasource() { |
|
|
|
|
|
|
|
return datasource; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setDatasource(DruidDataSource datasource) { |
|
|
|
|
|
|
|
this.datasource = datasource; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public String getDiscardIdleConnectionsSeconds() { |
|
|
|
|
|
|
|
return discardIdleConnectionsSeconds; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void setDiscardIdleConnectionsSeconds(String discardIdleConnectionsSeconds) { |
|
|
|
|
|
|
|
this.discardIdleConnectionsSeconds = discardIdleConnectionsSeconds; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|