优化多数据源

This commit is contained in:
zhouhao
2016-09-01 18:28:23 +08:00
parent 69e6c9a04d
commit 3672bbd102
9 changed files with 95 additions and 23 deletions

2
.gitignore vendored
View File

@@ -15,6 +15,6 @@
*.jar
*.war
*.ear
*.log
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

View File

@@ -17,7 +17,10 @@
package org.hsweb.web.core.datasource;
import org.hsweb.web.core.utils.ThreadLocalUtils;
import org.springframework.jca.cci.connection.ConnectionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import javax.sql.CommonDataSource;
import javax.sql.DataSource;
/**
@@ -50,5 +53,5 @@ public interface DynamicDataSource extends DataSource {
useDefault(true);
}
DataSource getActiveDataSource();
CommonDataSource getActiveDataSource();
}

View File

@@ -47,6 +47,37 @@
<version>1.0.5</version>
<scope>test</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>com.atomikos</groupId>-->
<!--<artifactId>transactions-jdbc</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.atomikos</groupId>-->
<!--<artifactId>transactions-jta</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.atomikos</groupId>-->
<!--<artifactId>transactions</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.atomikos</groupId>-->
<!--<artifactId>transactions-api</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.atomikos</groupId>-->
<!--<artifactId>atomikos-util</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>javax.transaction</groupId>-->
<!--<artifactId>jta</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.hsweb</groupId>
<artifactId>hsweb-expands-office</artifactId>

View File

@@ -18,7 +18,6 @@ package org.hsweb.web.service.impl;
import org.hsweb.web.service.impl.datasource.DynamicDataSourceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -47,7 +46,7 @@ public class DynamicDataSourceAutoConfiguration {
if (this.properties.getType() != null) {
factory.type(this.properties.getType());
}
return new DynamicDataSourceImpl(factory.build());
}
}

View File

@@ -27,7 +27,7 @@ import java.sql.SQLException;
public class DynamicDataSourceImpl extends AbstractDataSource implements DynamicDataSource {
private javax.sql.DataSource defaultDataSource;
private DynamicDataSourceService dynamicDataSourceService;
protected DynamicDataSourceService dynamicDataSourceService;
public DynamicDataSourceImpl(javax.sql.DataSource defaultDataSource) {
this.defaultDataSource = defaultDataSource;
@@ -58,4 +58,5 @@ public class DynamicDataSourceImpl extends AbstractDataSource implements Dynamic
public void setDynamicDataSourceService(DynamicDataSourceService dynamicDataSourceService) {
this.dynamicDataSourceService = dynamicDataSourceService;
}
}

View File

@@ -25,7 +25,9 @@ import org.hsweb.web.service.datasource.DataSourceService;
import org.hsweb.web.service.datasource.DynamicDataSourceService;
import org.hsweb.web.service.impl.basic.SqlExecutorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@@ -39,7 +41,7 @@ public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {
@Resource
private DataSourceService dataSourceService;
@Autowired
@Autowired(required = false)
private DynamicDataSource dynamicDataSource;
@Autowired
@@ -52,6 +54,10 @@ public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {
return getCache(id).getDataSource();
}
public PlatformTransactionManager getTransactionManager(String id) {
return getCache(id).getTransactionManager();
}
@Override
public SqlExecutor getSqlExecutor(String id) {
return getCache(id).getSqlExecutor();
@@ -90,7 +96,8 @@ public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {
@PostConstruct
public void init() {
((DynamicDataSourceImpl) dynamicDataSource).setDynamicDataSourceService(this);
if (null != dynamicDataSource)
((DynamicDataSourceImpl) dynamicDataSource).setDynamicDataSourceService(this);
}
class CacheInfo {
@@ -98,12 +105,15 @@ public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {
javax.sql.DataSource dataSource;
PlatformTransactionManager transactionManager;
SqlExecutor sqlExecutor;
public CacheInfo(int hash, javax.sql.DataSource dataSource) {
this.hash = hash;
this.dataSource = dataSource;
sqlExecutor = new SqlExecutorService().setDataSource(dataSource);
transactionManager = new DataSourceTransactionManager(dataSource);
}
public int getHash() {
@@ -117,5 +127,10 @@ public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {
public SqlExecutor getSqlExecutor() {
return sqlExecutor;
}
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}
}
}

View File

@@ -1,25 +1,10 @@
package org.hsweb.web.service.impl.system;
import com.alibaba.fastjson.JSON;
import org.hsweb.ezorm.executor.SqlExecutor;
import org.hsweb.web.bean.common.DeleteParam;
import org.hsweb.web.bean.common.InsertParam;
import org.hsweb.web.bean.common.QueryParam;
import org.hsweb.web.bean.common.UpdateParam;
import org.hsweb.web.bean.po.role.RoleModule;
import org.hsweb.web.bean.po.user.User;
import org.hsweb.web.dao.role.RoleModuleMapper;
import org.hsweb.web.dao.user.UserMapper;
import org.hsweb.web.service.impl.AbstractTestCase;
import org.hsweb.web.service.module.ModuleMetaService;
import org.hsweb.web.service.system.DataBaseManagerService;
import org.junit.Test;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* Created by zhouhao on 16-4-21.
@@ -35,5 +20,4 @@ public class DataBaseManagerServiceImplTest extends AbstractTestCase {
}
}

View File

@@ -19,6 +19,7 @@ package org.hsweb.web.service.datasource;
import org.hsweb.ezorm.executor.SqlExecutor;
import javax.sql.DataSource;
import javax.sql.XADataSource;
/**
* @author zhouhao
@@ -29,4 +30,5 @@ public interface DynamicDataSourceService {
SqlExecutor getSqlExecutor(String id);
Object getTransactionManager(String id);
}

37
pom.xml
View File

@@ -44,6 +44,7 @@
<hsweb.ezorm.version>1.0-SNAPSHOT</hsweb.ezorm.version>
<hsweb.commons.version>1.0-SNAPSHOT</hsweb.commons.version>
<hsweb.expands.version>1.0-SNAPSHOT</hsweb.expands.version>
<atomikos.version>4.0.4</atomikos.version>
</properties>
<build>
@@ -113,6 +114,42 @@
<dependencyManagement>
<dependencies>
<!--atomikos-->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-api</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>atomikos-util</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!--oauth2-->
<dependency>