Merge pull request #25 from kissss/datasource

feature:can use existing DataSource
This commit is contained in:
纳兰丶 2018-07-20 18:09:22 +08:00 committed by GitHub
commit a54177bfb4
3 changed files with 34 additions and 20 deletions

View File

@ -113,4 +113,11 @@ public class TccDbConfig {
* Add a property (name/value pair) that will be used to configure the {@link DataSource}/{@link java.sql.Driver}.
*/
private Map<String, Object> dataSourcePropertyMap;
/**
* You can use a existing DataSource or generate a new DataSource based on the configuration.
*/
private DataSource dataSource;
}

View File

@ -35,6 +35,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
@ -54,7 +55,7 @@ public class JdbcCoordinatorRepository implements CoordinatorRepository {
private Logger logger = LoggerFactory.getLogger(JdbcCoordinatorRepository.class);
private HikariDataSource dataSource;
private DataSource dataSource;
private String tableName;
@ -195,22 +196,27 @@ public class JdbcCoordinatorRepository implements CoordinatorRepository {
@Override
public void init(final String modelName, final TccConfig txConfig) {
final TccDbConfig tccDbConfig = txConfig.getTccDbConfig();
dataSource = new HikariDataSource();
dataSource.setJdbcUrl(tccDbConfig.getUrl());
dataSource.setDriverClassName(tccDbConfig.getDriverClassName());
dataSource.setUsername(tccDbConfig.getUsername());
dataSource.setPassword(tccDbConfig.getPassword());
dataSource.setMaximumPoolSize(tccDbConfig.getMaxActive());
dataSource.setMinimumIdle(tccDbConfig.getMinIdle());
dataSource.setConnectionTimeout(tccDbConfig.getConnectionTimeout());
dataSource.setIdleTimeout(tccDbConfig.getIdleTimeout());
dataSource.setMaxLifetime(tccDbConfig.getMaxLifetime());
dataSource.setConnectionTestQuery(tccDbConfig.getConnectionTestQuery());
if (tccDbConfig.getDataSourcePropertyMap() != null && !tccDbConfig.getDataSourcePropertyMap().isEmpty()) {
tccDbConfig.getDataSourcePropertyMap().forEach((key, value) -> dataSource.addDataSourceProperty(key, value));
if (tccDbConfig.getDataSource() != null) {
dataSource = tccDbConfig.getDataSource();
} else {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(tccDbConfig.getUrl());
hikariDataSource.setDriverClassName(tccDbConfig.getDriverClassName());
hikariDataSource.setUsername(tccDbConfig.getUsername());
hikariDataSource.setPassword(tccDbConfig.getPassword());
hikariDataSource.setMaximumPoolSize(tccDbConfig.getMaxActive());
hikariDataSource.setMinimumIdle(tccDbConfig.getMinIdle());
hikariDataSource.setConnectionTimeout(tccDbConfig.getConnectionTimeout());
hikariDataSource.setIdleTimeout(tccDbConfig.getIdleTimeout());
hikariDataSource.setMaxLifetime(tccDbConfig.getMaxLifetime());
hikariDataSource.setConnectionTestQuery(tccDbConfig.getConnectionTestQuery());
if (tccDbConfig.getDataSourcePropertyMap() != null && !tccDbConfig.getDataSourcePropertyMap().isEmpty()) {
tccDbConfig.getDataSourcePropertyMap().forEach(hikariDataSource::addDataSourceProperty);
}
dataSource = hikariDataSource;
}
this.tableName = RepositoryPathUtils.buildDbTableName(modelName);
//save current database type
// //save current database type
this.currentDBType = DbTypeUtils.buildByDriverClassName(tccDbConfig.getDriverClassName());
executeUpdate(SqlHelper.buildCreateTableSql(tccDbConfig.getDriverClassName(), tableName));
}

View File

@ -24,11 +24,12 @@
<property name="repositorySupport" value="db"/>
<property name="tccDbConfig">
<bean class="com.hmily.tcc.common.config.TccDbConfig">
<property name="url"
value="jdbc:mysql://10.4.4.202:3306/tcc?useUnicode=true&amp;characterEncoding=utf8"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="123123"/>
<property name="dataSource" ref="dataSource"/>
<!--<property name="url"-->
<!--value="jdbc:mysql://localhost:3306/tcc?useUnicode=true&amp;characterEncoding=utf8"/>-->
<!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
<!--<property name="username" value="root"/>-->
<!--<property name="password" value="123456"/>-->
</bean>
</property>
</bean>