Browse Source

add druid connection provider (#699)

* add druid connection provider in order to change quartz default connection pool

* remove c3p0 in escheduler-api and escheduler-dao

* change method name queryByIdAndIp to queryBySessionId and queryByUserIdAndIp to queryByUserId

* change connectionProvider.class from c3p0 to druid

* change connectionProvider.class from c3p0 to druid
pull/2/head
lgcareer 5 years ago committed by bao liang
parent
commit
9406103a03
  1. 6
      escheduler-api/pom.xml
  2. 2
      escheduler-common/src/main/resources/quartz.properties
  3. 6
      escheduler-dao/pom.xml
  4. 8
      escheduler-dao/src/main/java/cn/escheduler/dao/mapper/SessionMapperProvider.java
  5. 203
      escheduler-server/src/main/java/cn/escheduler/server/quartz/DruidConnectionProvider.java

6
escheduler-api/pom.xml

@ -141,6 +141,12 @@
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<exclusions>
<exclusion>
<artifactId>c3p0</artifactId>
<groupId>c3p0</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>

2
escheduler-common/src/main/resources/quartz.properties

@ -30,7 +30,7 @@ org.quartz.jobStore.dataSource = myDs
#============================================================================
# Configure Datasources
#============================================================================
org.quartz.dataSource.myDs.connectionProvider.class = cn.escheduler.server.quartz.DruidConnectionProvider
org.quartz.dataSource.myDs.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDs.URL = jdbc:mysql://192.168.xx.xx:3306/escheduler?characterEncoding=utf8
org.quartz.dataSource.myDs.user = xx

6
escheduler-dao/pom.xml

@ -116,6 +116,12 @@
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<exclusions>
<exclusion>
<artifactId>c3p0</artifactId>
<groupId>c3p0</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>

8
escheduler-dao/src/main/java/cn/escheduler/dao/mapper/SessionMapperProvider.java

@ -103,11 +103,11 @@ public class SessionMapperProvider {
}
/**
* query by session id and ip
* query by session id
* @param parameter
* @return
*/
public String queryByIdAndIp(Map<String, Object> parameter) {
public String queryBySessionId(Map<String, Object> parameter) {
return new SQL() {{
SELECT("*");
@ -118,11 +118,11 @@ public class SessionMapperProvider {
}
/**
* query by user id and ip
* query by user id
* @param parameter
* @return
*/
public String queryByUserIdAndIp(Map<String, Object> parameter) {
public String queryByUserId(Map<String, Object> parameter) {
return new SQL() {{
SELECT("*");

203
escheduler-server/src/main/java/cn/escheduler/server/quartz/DruidConnectionProvider.java

@ -0,0 +1,203 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.escheduler.server.quartz;
import com.alibaba.druid.pool.DruidDataSource;
import org.quartz.SchedulerException;
import java.sql.Connection;
import java.sql.SQLException;
import org.quartz.utils.ConnectionProvider;
/**
* druid connection provider
*/
public class DruidConnectionProvider implements ConnectionProvider {
/**
* 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;
/**
* The maximum number of prepared statements that will be cached per connection in the pool.
*/
public static final int DEFAULT_DB_MAX_CACHED_STATEMENTS_PER_CONNECTION = 120;
/**
* Druid connection pool
*/
private DruidDataSource datasource;
public Connection getConnection() throws SQLException {
return datasource.getConnection();
}
public void shutdown() throws SQLException {
datasource.close();
}
public void initialize() throws SQLException{
if (this.URL == null) {
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.getMessage(), 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;
}
}
Loading…
Cancel
Save