mirror of
https://gitee.com/dromara/hmily.git
synced 2024-12-05 04:39:09 +08:00
Merge pull request #25 from kissss/datasource
feature:can use existing DataSource
This commit is contained in:
commit
a54177bfb4
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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&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&characterEncoding=utf8"/>-->
|
||||
<!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
|
||||
<!--<property name="username" value="root"/>-->
|
||||
<!--<property name="password" value="123456"/>-->
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
Loading…
Reference in New Issue
Block a user