modify
This commit is contained in:
parent
5d14433073
commit
f6fbda5859
@ -1,47 +0,0 @@
|
||||
package com.heibaiying.springboot.config;
|
||||
|
||||
import com.atomikos.jdbc.AtomikosDataSourceBean;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public class AbstractDataSourceConfig {
|
||||
|
||||
public DataSource getDataSource(Environment env, String prefix, String dataSourceName){
|
||||
Properties prop = build(env,prefix);
|
||||
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
||||
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
|
||||
ds.setUniqueResourceName(dataSourceName);
|
||||
ds.setXaProperties(prop);
|
||||
return ds;
|
||||
}
|
||||
|
||||
public Properties build(Environment env, String prefix) {
|
||||
Properties prop = new Properties();
|
||||
prop.put("url", env.getProperty(prefix + "url"));
|
||||
prop.put("username", env.getProperty(prefix + "username"));
|
||||
prop.put("password", env.getProperty(prefix + "password"));
|
||||
prop.put("driverClassName", env.getProperty(prefix + "driver-class-name", ""));
|
||||
prop.put("initialSize", env.getProperty(prefix + "initialSize", Integer.class));
|
||||
prop.put("maxActive", env.getProperty(prefix + "maxActive", Integer.class));
|
||||
prop.put("minIdle", env.getProperty(prefix + "minIdle", Integer.class));
|
||||
prop.put("maxWait", env.getProperty(prefix + "maxWait", Integer.class));
|
||||
//prop.put("poolPreparedStatements", env.getProperty(prefix + "poolPreparedStatements", Boolean.class));
|
||||
//prop.put("maxPoolPreparedStatementPerConnectionSize",env.getProperty(prefix + "maxPoolPreparedStatementPerConnectionSize", Integer.class));
|
||||
prop.put("validationQuery", env.getProperty(prefix + "validationQuery"));
|
||||
//prop.put("validationQueryTimeout", env.getProperty(prefix + "validationQueryTimeout", Integer.class));
|
||||
prop.put("testOnBorrow", env.getProperty(prefix + "testOnBorrow", Boolean.class));
|
||||
prop.put("testOnReturn", env.getProperty(prefix + "testOnReturn", Boolean.class));
|
||||
prop.put("testWhileIdle", env.getProperty(prefix + "testWhileIdle", Boolean.class));
|
||||
prop.put("timeBetweenEvictionRunsMillis", env.getProperty(prefix + "timeBetweenEvictionRunsMillis", Integer.class));
|
||||
prop.put("minEvictableIdleTimeMillis", env.getProperty(prefix + "minEvictableIdleTimeMillis", Integer.class));
|
||||
//prop.put("useGlobalDataSourceStat",env.getProperty(prefix + "useGlobalDataSourceStat", Boolean.class));
|
||||
//prop.put("filters", env.getProperty(prefix + "filters"));
|
||||
return prop;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.heibaiying.springboot.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.cursor.Cursor;
|
||||
import org.apache.ibatis.exceptions.PersistenceException;
|
||||
import org.apache.ibatis.executor.BatchResult;
|
||||
import org.apache.ibatis.session.*;
|
||||
@ -26,7 +27,6 @@ import static org.mybatis.spring.SqlSessionUtils.*;
|
||||
|
||||
@Slf4j
|
||||
public class CustomSqlSessionTemplate extends SqlSessionTemplate {
|
||||
/*
|
||||
|
||||
|
||||
private final SqlSessionFactory sqlSessionFactory;
|
||||
@ -72,16 +72,15 @@ public class CustomSqlSessionTemplate extends SqlSessionTemplate {
|
||||
this.defaultTargetSqlSessionFactory = defaultTargetSqlSessionFactory;
|
||||
}
|
||||
|
||||
*/
|
||||
/***
|
||||
/***
|
||||
* 获取当前使用数据源对应的会话工厂
|
||||
*//*
|
||||
*/
|
||||
|
||||
@Override
|
||||
public SqlSessionFactory getSqlSessionFactory() {
|
||||
|
||||
String dataSourceKey = DataSourceContextHolder.getDataKey();
|
||||
log.info("SqlSessionFactory key : {}",dataSourceKey);
|
||||
log.info("当前会话工厂 : {}", dataSourceKey);
|
||||
SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactories.get(dataSourceKey);
|
||||
if (targetSqlSessionFactory != null) {
|
||||
return targetSqlSessionFactory;
|
||||
@ -95,10 +94,9 @@ public class CustomSqlSessionTemplate extends SqlSessionTemplate {
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
/**
|
||||
/**
|
||||
* 这个方法的实现和父类的实现是基本一致的,唯一不同的就是在getSqlSession方法传参中获取会话工厂的方式
|
||||
*//*
|
||||
*/
|
||||
|
||||
private class SqlSessionInterceptor implements InvocationHandler {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
@ -137,304 +135,170 @@ public class CustomSqlSessionTemplate extends SqlSessionTemplate {
|
||||
return this.exceptionTranslator;
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <T> T selectOne(String statement) {
|
||||
return this.sqlSessionProxy.<T> selectOne(statement);
|
||||
return this.sqlSessionProxy.<T>selectOne(statement);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <T> T selectOne(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.<T> selectOne(statement, parameter);
|
||||
return this.sqlSessionProxy.<T>selectOne(statement, parameter);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
|
||||
return this.sqlSessionProxy.<K, V> selectMap(statement, mapKey);
|
||||
return this.sqlSessionProxy.<K, V>selectMap(statement, mapKey);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
|
||||
return this.sqlSessionProxy.<K, V> selectMap(statement, parameter, mapKey);
|
||||
return this.sqlSessionProxy.<K, V>selectMap(statement, parameter, mapKey);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.<K, V> selectMap(statement, parameter, mapKey, rowBounds);
|
||||
return this.sqlSessionProxy.<K, V>selectMap(statement, parameter, mapKey, rowBounds);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <T> Cursor<T> selectCursor(String statement) {
|
||||
return this.sqlSessionProxy.selectCursor(statement);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <T> Cursor<T> selectCursor(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.selectCursor(statement, parameter);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <E> List<E> selectList(String statement) {
|
||||
return this.sqlSessionProxy.<E> selectList(statement);
|
||||
return this.sqlSessionProxy.<E>selectList(statement);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <E> List<E> selectList(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.<E> selectList(statement, parameter);
|
||||
return this.sqlSessionProxy.<E>selectList(statement, parameter);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.<E> selectList(statement, parameter, rowBounds);
|
||||
return this.sqlSessionProxy.<E>selectList(statement, parameter, rowBounds);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void select(String statement, ResultHandler handler) {
|
||||
this.sqlSessionProxy.select(statement, handler);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void select(String statement, Object parameter, ResultHandler handler) {
|
||||
this.sqlSessionProxy.select(statement, parameter, handler);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
|
||||
this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public int insert(String statement) {
|
||||
return this.sqlSessionProxy.insert(statement);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public int insert(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.insert(statement, parameter);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public int update(String statement) {
|
||||
return this.sqlSessionProxy.update(statement);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public int update(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.update(statement, parameter);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public int delete(String statement) {
|
||||
return this.sqlSessionProxy.delete(statement);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public int delete(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.delete(statement, parameter);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public <T> T getMapper(Class<T> type) {
|
||||
return getConfiguration().getMapper(type, this);
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void commit() {
|
||||
throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void commit(boolean force) {
|
||||
throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void rollback() {
|
||||
throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void rollback(boolean force) {
|
||||
throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public void clearCache() {
|
||||
this.sqlSessionProxy.clearCache();
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public Configuration getConfiguration() {
|
||||
return this.sqlSessionFactory.getConfiguration();
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return this.sqlSessionProxy.getConnection();
|
||||
}
|
||||
|
||||
*/
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 1.0.2
|
||||
*
|
||||
*//*
|
||||
|
||||
@Override
|
||||
public List<BatchResult> flushStatements() {
|
||||
@ -443,297 +307,7 @@ public class CustomSqlSessionTemplate extends SqlSessionTemplate {
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
//This method forces spring disposer to avoid call of SqlSessionTemplate.close() which gives UnsupportedOperationException
|
||||
}
|
||||
*/
|
||||
private final SqlSessionFactory sqlSessionFactory;
|
||||
private final ExecutorType executorType;
|
||||
private final SqlSession sqlSessionProxy;
|
||||
private final PersistenceExceptionTranslator exceptionTranslator;
|
||||
|
||||
private Map<Object, SqlSessionFactory> targetSqlSessionFactorys;
|
||||
private SqlSessionFactory defaultTargetSqlSessionFactory;
|
||||
|
||||
public void setTargetSqlSessionFactorys(Map<Object, SqlSessionFactory> targetSqlSessionFactorys) {
|
||||
this.targetSqlSessionFactorys = targetSqlSessionFactorys;
|
||||
}
|
||||
|
||||
public void setDefaultTargetSqlSessionFactory(SqlSessionFactory defaultTargetSqlSessionFactory) {
|
||||
this.defaultTargetSqlSessionFactory = defaultTargetSqlSessionFactory;
|
||||
}
|
||||
|
||||
public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
|
||||
this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
|
||||
}
|
||||
|
||||
public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
|
||||
this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration()
|
||||
.getEnvironment().getDataSource(), true));
|
||||
}
|
||||
|
||||
public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
|
||||
PersistenceExceptionTranslator exceptionTranslator) {
|
||||
|
||||
super(sqlSessionFactory, executorType, exceptionTranslator);
|
||||
|
||||
this.sqlSessionFactory = sqlSessionFactory;
|
||||
this.executorType = executorType;
|
||||
this.exceptionTranslator = exceptionTranslator;
|
||||
|
||||
this.sqlSessionProxy = (SqlSession) newProxyInstance(
|
||||
SqlSessionFactory.class.getClassLoader(),
|
||||
new Class[] { SqlSession.class },
|
||||
new SqlSessionInterceptor());
|
||||
|
||||
this.defaultTargetSqlSessionFactory = sqlSessionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSessionFactory getSqlSessionFactory() {
|
||||
|
||||
String dataSourceKey = DataSourceContextHolder.getDataKey();
|
||||
System.out.println("当前会话工厂 : "+dataSourceKey);
|
||||
SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactorys.get(dataSourceKey);
|
||||
if (targetSqlSessionFactory != null) {
|
||||
return targetSqlSessionFactory;
|
||||
} else if (defaultTargetSqlSessionFactory != null) {
|
||||
return defaultTargetSqlSessionFactory;
|
||||
} else {
|
||||
Assert.notNull(targetSqlSessionFactorys, "Property 'targetSqlSessionFactorys' or 'defaultTargetSqlSessionFactory' are required");
|
||||
Assert.notNull(defaultTargetSqlSessionFactory, "Property 'defaultTargetSqlSessionFactory' or 'targetSqlSessionFactorys' are required");
|
||||
}
|
||||
return this.sqlSessionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Configuration getConfiguration() {
|
||||
return this.getSqlSessionFactory().getConfiguration();
|
||||
}
|
||||
|
||||
public ExecutorType getExecutorType() {
|
||||
return this.executorType;
|
||||
}
|
||||
|
||||
public PersistenceExceptionTranslator getPersistenceExceptionTranslator() {
|
||||
return this.exceptionTranslator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <T> T selectOne(String statement) {
|
||||
return this.sqlSessionProxy.<T> selectOne(statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <T> T selectOne(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.<T> selectOne(statement, parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
|
||||
return this.sqlSessionProxy.<K, V> selectMap(statement, mapKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
|
||||
return this.sqlSessionProxy.<K, V> selectMap(statement, parameter, mapKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.<K, V> selectMap(statement, parameter, mapKey, rowBounds);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <E> List<E> selectList(String statement) {
|
||||
return this.sqlSessionProxy.<E> selectList(statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <E> List<E> selectList(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.<E> selectList(statement, parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.<E> selectList(statement, parameter, rowBounds);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void select(String statement, ResultHandler handler) {
|
||||
this.sqlSessionProxy.select(statement, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void select(String statement, Object parameter, ResultHandler handler) {
|
||||
this.sqlSessionProxy.select(statement, parameter, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
|
||||
this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int insert(String statement) {
|
||||
return this.sqlSessionProxy.insert(statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int insert(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.insert(statement, parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int update(String statement) {
|
||||
return this.sqlSessionProxy.update(statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int update(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.update(statement, parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int delete(String statement) {
|
||||
return this.sqlSessionProxy.delete(statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int delete(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.delete(statement, parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public <T> T getMapper(Class<T> type) {
|
||||
return getConfiguration().getMapper(type, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void commit() {
|
||||
throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void commit(boolean force) {
|
||||
throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void rollback() {
|
||||
throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void rollback(boolean force) {
|
||||
throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void close() {
|
||||
throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void clearCache() {
|
||||
this.sqlSessionProxy.clearCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public Connection getConnection() {
|
||||
return this.sqlSessionProxy.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public List<BatchResult> flushStatements() {
|
||||
return this.sqlSessionProxy.flushStatements();
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy needed to route MyBatis method calls to the proper SqlSession got from Spring's Transaction Manager It also
|
||||
* unwraps exceptions thrown by {@code Method#invoke(Object, Object...)} to pass a {@code PersistenceException} to
|
||||
* the {@code PersistenceExceptionTranslator}.
|
||||
*/
|
||||
private class SqlSessionInterceptor implements InvocationHandler {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
final SqlSession sqlSession = getSqlSession(
|
||||
CustomSqlSessionTemplate.this.getSqlSessionFactory(),
|
||||
CustomSqlSessionTemplate.this.executorType,
|
||||
CustomSqlSessionTemplate.this.exceptionTranslator);
|
||||
try {
|
||||
Object result = method.invoke(sqlSession, args);
|
||||
if (!isSqlSessionTransactional(sqlSession, CustomSqlSessionTemplate.this.getSqlSessionFactory())) {
|
||||
// force commit even on non-dirty sessions because some databases require
|
||||
// a commit/rollback before calling close()
|
||||
sqlSession.commit(true);
|
||||
}
|
||||
return result;
|
||||
} catch (Throwable t) {
|
||||
Throwable unwrapped = unwrapThrowable(t);
|
||||
if (CustomSqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
|
||||
Throwable translated = CustomSqlSessionTemplate.this.exceptionTranslator
|
||||
.translateExceptionIfPossible((PersistenceException) unwrapped);
|
||||
if (translated != null) {
|
||||
unwrapped = translated;
|
||||
}
|
||||
}
|
||||
throw unwrapped;
|
||||
} finally {
|
||||
closeSqlSession(sqlSession, CustomSqlSessionTemplate.this.getSqlSessionFactory());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.heibaiying.springboot.config;
|
||||
|
||||
import com.alibaba.druid.pool.xa.DruidXADataSource;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import com.atomikos.jdbc.AtomikosDataSourceBean;
|
||||
import com.heibaiying.springboot.constant.Data;
|
||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
@ -10,8 +10,6 @@ import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@ -22,46 +20,57 @@ import java.util.Map;
|
||||
* @author : heibaiying
|
||||
* @description : 多数据源配置
|
||||
* 需要特别强调的是以下任意的DataSource的bean都不要用@Primary去修饰,包括动态数据源。原因是在事务下,spring在DataSourceUtils类中doGetConnection方法中判断获取当前数据源是否持有连接的时候,
|
||||
* 总是会传入用@Primary修饰的数据源,如果一个事务下有多个数据源,这种情况下就会出现connection复用,无法切换数据源, 源码截图详见README.md
|
||||
* 总是会传入用@Primary修饰的数据源,如果一个事务下有多个数据源,这种情况下就会出现connection复用,无法切换数据源, 详见README.md
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan(basePackages = DataSourceConfig.BASE_PACKAGES, sqlSessionTemplateRef = "sqlSessionTemplate")
|
||||
public class DataSourceConfig extends AbstractDataSourceConfig {
|
||||
@MapperScan(basePackages = DataSourceFactory.BASE_PACKAGES, sqlSessionTemplateRef = "sqlSessionTemplate")
|
||||
public class DataSourceFactory {
|
||||
|
||||
static final String BASE_PACKAGES = "com.heibaiying.springboot.dao";
|
||||
|
||||
private static final String MAPPER_LOCATION = "classpath:mappers/*.xml";
|
||||
|
||||
/**
|
||||
* 创建数据源1,不要用@Primary去修饰
|
||||
|
||||
/***
|
||||
* 创建 DruidXADataSource 1
|
||||
*/
|
||||
@Bean
|
||||
public DataSource dataSourceOne(Environment env) {
|
||||
String prefix = "spring.datasource.druid.db1.";
|
||||
return getDataSource(env, prefix, "one");
|
||||
@ConfigurationProperties("spring.datasource.druid.db1")
|
||||
public DataSource DruidDataSourceOne() {
|
||||
return new DruidXADataSource();
|
||||
}
|
||||
|
||||
/***
|
||||
* 创建 DruidXADataSource 2
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.db2")
|
||||
public DataSource DruidDataSourceTwo() {
|
||||
return new DruidXADataSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据源2,不要用@Primary去修饰
|
||||
* 创建支持XA事务的数据源1,不要用@Primary去修饰
|
||||
*/
|
||||
@Bean
|
||||
public DataSource dataSourceTwo(Environment env) {
|
||||
String prefix = "spring.datasource.druid.db2.";
|
||||
return getDataSource(env, prefix, "two");
|
||||
public DataSource dataSourceOne(DataSource DruidDataSourceOne) {
|
||||
AtomikosDataSourceBean sourceBean = new AtomikosDataSourceBean();
|
||||
sourceBean.setXaDataSource((DruidXADataSource) DruidDataSourceOne);
|
||||
sourceBean.setUniqueResourceName("db1");
|
||||
return sourceBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建支持XA事务的数据源2,不要用@Primary去修饰
|
||||
*/
|
||||
@Bean
|
||||
public DataSource dataSourceTwo(DataSource DruidDataSourceTwo) {
|
||||
AtomikosDataSourceBean sourceBean = new AtomikosDataSourceBean();
|
||||
sourceBean.setXaDataSource((DruidXADataSource) DruidDataSourceTwo);
|
||||
sourceBean.setUniqueResourceName("db2");
|
||||
return sourceBean;
|
||||
}
|
||||
|
||||
/* @Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.db1")
|
||||
public DataSource dataSourceOne() {
|
||||
return new DruidXADataSource();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.db2")
|
||||
public DataSource dataSourceTwo() {
|
||||
return new DruidXADataSource();
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 设置多数据源,不要用@Primary去修饰
|
||||
@ -117,7 +126,7 @@ public class DataSourceConfig extends AbstractDataSourceConfig {
|
||||
sqlSessionFactoryMap.put(Data.DATASOURCE2, sqlSessionFactoryTwo);
|
||||
|
||||
CustomSqlSessionTemplate customSqlSessionTemplate = new CustomSqlSessionTemplate(sqlSessionFactoryOne);
|
||||
customSqlSessionTemplate.setTargetSqlSessionFactorys(sqlSessionFactoryMap);
|
||||
customSqlSessionTemplate.setTargetSqlSessionFactories(sqlSessionFactoryMap);
|
||||
return customSqlSessionTemplate;
|
||||
}
|
||||
|
@ -1,16 +1,17 @@
|
||||
package com.heibaiying.springboot.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 动态数据源配置
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
System.out.println("当前数据库:"+DataSourceContextHolder.getDataKey());
|
||||
log.info("当前数据库:{}",DataSourceContextHolder.getDataKey());
|
||||
return DataSourceContextHolder.getDataKey();
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import javax.transaction.UserTransaction;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 分布式事务配置
|
||||
* @description : JTA事务配置
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
|
@ -6,8 +6,6 @@ spring:
|
||||
username: root
|
||||
password: root
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# 使用 druid 作为连接池
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
|
||||
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
|
||||
initialSize: 5
|
||||
@ -22,7 +20,7 @@ spring:
|
||||
# 连接保持空闲而不被驱逐的最小时间
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
# 用来检测连接是否有效的sql 因数据库方言而异, 例如 oracle 应该写成 SELECT 1 FROM DUAL
|
||||
validationQuery: SELECT 9
|
||||
validationQuery: SELECT 1
|
||||
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
|
||||
testWhileIdle: true
|
||||
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
|
||||
@ -32,38 +30,18 @@ spring:
|
||||
# 是否自动回收超时连接
|
||||
removeAbandoned: true
|
||||
# 超时时间(以秒数为单位)
|
||||
remove-abandoned-timeout: 180
|
||||
# WebStatFilter用于采集web-jdbc关联监控的数据。
|
||||
web-stat-filter:
|
||||
# 是否开启 WebStatFilter 默认是true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /*
|
||||
# 排除静态资源的请求
|
||||
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
|
||||
remove-abandoned-timeout: 1800
|
||||
|
||||
# Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。
|
||||
stat-view-servlet:
|
||||
#是否启用StatViewServlet 默认值true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /druid/*
|
||||
# 允许清空统计数据
|
||||
reset-enable: true
|
||||
login-username: druid
|
||||
login-password: druid
|
||||
db2:
|
||||
url: jdbc:mysql://127.0.0.1:3306/mysql02?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# 使用 druid 作为连接池
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
|
||||
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
|
||||
initialSize: 5
|
||||
initialSize: 6
|
||||
# 最小连接池数量
|
||||
minIdle: 5
|
||||
minIdle: 6
|
||||
# 最大连接池数量
|
||||
maxActive: 10
|
||||
# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
|
||||
@ -73,7 +51,7 @@ spring:
|
||||
# 连接保持空闲而不被驱逐的最小时间
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
# 用来检测连接是否有效的sql 因数据库方言而异, 例如 oracle 应该写成 SELECT 1 FROM DUAL
|
||||
validationQuery: SELECT 9
|
||||
validationQuery: SELECT 1
|
||||
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
|
||||
testWhileIdle: true
|
||||
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
|
||||
@ -83,31 +61,23 @@ spring:
|
||||
# 是否自动回收超时连接
|
||||
removeAbandoned: true
|
||||
# 超时时间(以秒数为单位)
|
||||
remove-abandoned-timeout: 180
|
||||
# WebStatFilter用于采集web-jdbc关联监控的数据。
|
||||
web-stat-filter:
|
||||
# 是否开启 WebStatFilter 默认是true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /*
|
||||
# 排除静态资源的请求
|
||||
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
|
||||
remove-abandoned-timeout: 1800
|
||||
# WebStatFilter用于采集web-jdbc关联监控的数据。
|
||||
web-stat-filter:
|
||||
# 是否开启 WebStatFilter 默认是true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /*
|
||||
# 排除静态资源的请求
|
||||
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
|
||||
|
||||
# Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。
|
||||
stat-view-servlet:
|
||||
#是否启用StatViewServlet 默认值true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /druid/*
|
||||
# 允许清空统计数据
|
||||
reset-enable: true
|
||||
login-username: druid
|
||||
login-password: druid
|
||||
# mybatis 相关配置
|
||||
mybatis:
|
||||
configuration:
|
||||
# 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。
|
||||
# oracle数据库建议配置为JdbcType.NULL, 默认是Other
|
||||
jdbc-type-for-null: 'null'
|
||||
# 是否打印sql语句 调试的时候可以开启
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
# Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。
|
||||
stat-view-servlet:
|
||||
#是否启用StatViewServlet 默认值true
|
||||
enabled: true
|
||||
# 需要拦截的url
|
||||
url-pattern: /druid/*
|
||||
# 允许清空统计数据
|
||||
reset-enable: true
|
||||
login-username: druid
|
||||
login-password: druid
|
||||
|
@ -1,9 +0,0 @@
|
||||
{"id":"10.0.75.1.tm154840078074100001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"COMMITTING","expires":1548400791454,"resourceName":"one"},{"uri":"10.0.75.1.tm2","state":"COMMITTING","expires":1548400791454,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840078074100001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"TERMINATED","expires":1548400791469,"resourceName":"one"},{"uri":"10.0.75.1.tm2","state":"TERMINATED","expires":1548400791469,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840078467900002","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm3","state":"COMMITTING","expires":1548400794622,"resourceName":"one"},{"uri":"10.0.75.1.tm4","state":"COMMITTING","expires":1548400794622,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840078467900002","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm3","state":"TERMINATED","expires":1548400794642,"resourceName":"one"},{"uri":"10.0.75.1.tm4","state":"TERMINATED","expires":1548400794642,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840078978400003","wasCommitted":false,"participants":[{"uri":"10.0.75.1.tm5","state":"TERMINATED","expires":1548400799772,"resourceName":"one"},{"uri":"10.0.75.1.tm6","state":"TERMINATED","expires":1548400799772,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840079364000004","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm7","state":"COMMITTING","expires":1548400803592,"resourceName":"one"},{"uri":"10.0.75.1.tm8","state":"COMMITTING","expires":1548400803592,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840079364000004","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm7","state":"TERMINATED","expires":1548400803609,"resourceName":"one"},{"uri":"10.0.75.1.tm8","state":"TERMINATED","expires":1548400803609,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840079686400005","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm9","state":"COMMITTING","expires":1548400806814,"resourceName":"one"},{"uri":"10.0.75.1.tm10","state":"COMMITTING","expires":1548400806814,"resourceName":"two"}]}
|
||||
{"id":"10.0.75.1.tm154840079686400005","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm9","state":"TERMINATED","expires":1548400806831,"resourceName":"one"},{"uri":"10.0.75.1.tm10","state":"TERMINATED","expires":1548400806831,"resourceName":"two"}]}
|
15
spring-boot/springboot-druid-mybatis-multi/tmlog117.log
Normal file
15
spring-boot/springboot-druid-mybatis-multi/tmlog117.log
Normal file
@ -0,0 +1,15 @@
|
||||
{"id":"10.0.75.1.tm154841018873800001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"COMMITTING","expires":1548410199513,"resourceName":"db1"},{"uri":"10.0.75.1.tm2","state":"COMMITTING","expires":1548410199513,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841018873800001","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm1","state":"TERMINATED","expires":1548410199530,"resourceName":"db1"},{"uri":"10.0.75.1.tm2","state":"TERMINATED","expires":1548410199530,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841019225200002","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm3","state":"COMMITTING","expires":1548410202212,"resourceName":"db1"},{"uri":"10.0.75.1.tm4","state":"COMMITTING","expires":1548410202212,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841019225200002","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm3","state":"TERMINATED","expires":1548410202229,"resourceName":"db1"},{"uri":"10.0.75.1.tm4","state":"TERMINATED","expires":1548410202229,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841019386700003","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm5","state":"COMMITTING","expires":1548410203812,"resourceName":"db1"},{"uri":"10.0.75.1.tm6","state":"COMMITTING","expires":1548410203812,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841019386700003","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm5","state":"TERMINATED","expires":1548410203832,"resourceName":"db1"},{"uri":"10.0.75.1.tm6","state":"TERMINATED","expires":1548410203832,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841022094600004","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm7","state":"COMMITTING","expires":1548410230871,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841022094600004","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm7","state":"TERMINATED","expires":1548410230877,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841022770300005","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm8","state":"COMMITTING","expires":1548410237617,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841022770300005","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm8","state":"TERMINATED","expires":1548410237623,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841023952400006","wasCommitted":false,"participants":[{"uri":"10.0.75.1.tm9","state":"TERMINATED","expires":1548410249451,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841026359700007","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm10","state":"COMMITTING","expires":1548410273511,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841026359700007","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm10","state":"TERMINATED","expires":1548410273518,"resourceName":"db1"}]}
|
||||
{"id":"10.0.75.1.tm154841026807100008","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm11","state":"COMMITTING","expires":1548410278015,"resourceName":"db1"},{"uri":"10.0.75.1.tm12","state":"COMMITTING","expires":1548410278015,"resourceName":"db2"}]}
|
||||
{"id":"10.0.75.1.tm154841026807100008","wasCommitted":true,"participants":[{"uri":"10.0.75.1.tm11","state":"TERMINATED","expires":1548410278033,"resourceName":"db1"},{"uri":"10.0.75.1.tm12","state":"TERMINATED","expires":1548410278033,"resourceName":"db2"}]}
|
Loading…
x
Reference in New Issue
Block a user