去掉jta事务管理

This commit is contained in:
暮光:城中城
2020-04-08 12:17:15 +08:00
parent d87d4a7f70
commit 211eafaeed
8 changed files with 101 additions and 172 deletions

View File

@@ -41,10 +41,6 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>

View File

@@ -1,26 +1,19 @@
package com.zyplayer.doc.data.config; package com.zyplayer.doc.data.config;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.zyplayer.doc.data.repository.support.interceptor.SqlLogInterceptor; import com.zyplayer.doc.data.repository.support.interceptor.SqlLogInterceptor;
import com.zyplayer.doc.data.utils.DruidDataSourceUtil;
import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.sql.DataSource; import javax.sql.DataSource;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.Properties; import java.util.Properties;
/** /**
@@ -32,59 +25,18 @@ public class MybatisPlusConfig {
/** /**
* sql日志 * sql日志
**/ **/
private static final SqlLogInterceptor SQL_LOG_INTERCEPTOR; private static final SqlLogInterceptor SQL_LOG_INTERCEPTOR = new SqlLogInterceptor();
/** /**
* MYSQL 分页 * MYSQL 分页
**/ **/
private static final PageHelper MYSQL_PAGE_HELPER; private static final PageHelper MYSQL_PAGE_HELPER;
static { static {
{
MYSQL_PAGE_HELPER = new PageHelper(); MYSQL_PAGE_HELPER = new PageHelper();
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("dialect", "mysql"); properties.setProperty("dialect", "mysql");
MYSQL_PAGE_HELPER.setProperties(properties); MYSQL_PAGE_HELPER.setProperties(properties);
} }
{
SQL_LOG_INTERCEPTOR = new SqlLogInterceptor();
Properties properties = new Properties();
SQL_LOG_INTERCEPTOR.setProperties(properties);
}
}
/**
* 分布式事务配置
*/
@Configuration
static class JTATransactionManagerConfig {
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(300);
return userTransactionImp;
}
@Bean(name = "atomikosTransactionManager")
public TransactionManager atomikosTransactionManager() {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(true);
return userTransactionManager;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
TransactionManager atomikosTransactionManager = atomikosTransactionManager();
JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(userTransaction, atomikosTransactionManager);
jtaTransactionManager.setAllowCustomIsolationLevels(true);
jtaTransactionManager.setGlobalRollbackOnParticipationFailure(true);
jtaTransactionManager.setDefaultTimeout(30);
return jtaTransactionManager;
}
}
/** /**
* 数据库配置 * 数据库配置
@@ -105,24 +57,7 @@ public class MybatisPlusConfig {
@Bean(name = "manageDatasource") @Bean(name = "manageDatasource")
public DataSource manageDatasource() { public DataSource manageDatasource() {
Properties xaProperties = new Properties(); return DruidDataSourceUtil.createDataSource(driverClassName, url, username, password);
xaProperties.setProperty("driverClassName", driverClassName);
xaProperties.setProperty("url", url);
xaProperties.setProperty("username", username);
xaProperties.setProperty("password", password);
xaProperties.setProperty("maxActive", "500");
xaProperties.setProperty("testOnBorrow", "true");
xaProperties.setProperty("testWhileIdle", "true");
xaProperties.setProperty("validationQuery", "select 'x'");
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaProperties(xaProperties);
xaDataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
xaDataSource.setUniqueResourceName("manageDatasource");
xaDataSource.setMaxPoolSize(500);
xaDataSource.setMinPoolSize(1);
xaDataSource.setMaxLifetime(60);
return xaDataSource;
} }
@Bean(name = "manageSqlSessionFactory") @Bean(name = "manageSqlSessionFactory")
@@ -137,16 +72,6 @@ public class MybatisPlusConfig {
} }
} }
@Bean
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
/* <!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 --> */
performanceInterceptor.setMaxTime(1000);
/* <!--SQL是否格式化 默认false--> */
performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
@Bean @Bean
public PaginationInterceptor paginationInterceptor() { public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor(); return new PaginationInterceptor();

View File

@@ -0,0 +1,50 @@
package com.zyplayer.doc.data.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.zyplayer.doc.core.exception.ConfirmException;
import java.util.concurrent.atomic.AtomicLong;
public class DruidDataSourceUtil {
private static AtomicLong nameId = new AtomicLong(0);
public static DruidDataSource createDataSource(String driverClassName, String url, String username, String password){
try {
// 数据源配置
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(50);
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setTestOnReturn(false);
dataSource.setValidationQuery("select 1");
dataSource.setMaxWait(3000);
dataSource.setTimeBetweenEvictionRunsMillis(60000);
dataSource.setMinEvictableIdleTimeMillis(3600000);
// 重试3次失败退出源码里是errorCount > connectionErrorRetryAttempts所以写成2就是3次、、、
// CreateConnectionThread 源码在这个类里面
dataSource.setConnectionErrorRetryAttempts(2);
dataSource.setBreakAfterAcquireFailure(true);
dataSource.setName("zyplayer-doc-db-" + nameId.incrementAndGet());
if (url.contains("oracle")) {
dataSource.setValidationQuery("select 1 from dual");
}
DruidPooledConnection connection = dataSource.getConnection(3000);
if (connection == null) {
throw new ConfirmException("尝试获取该数据源连接失败:" + url);
}
connection.recycle();
return dataSource;
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -37,10 +37,6 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId> <artifactId>mybatis</artifactId>

View File

@@ -1,9 +1,8 @@
package com.zyplayer.doc.db.framework.configuration; package com.zyplayer.doc.db.framework.configuration;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.zyplayer.doc.core.exception.ConfirmException;
import com.zyplayer.doc.data.repository.manage.entity.DbDatasource; import com.zyplayer.doc.data.repository.manage.entity.DbDatasource;
import com.zyplayer.doc.data.utils.DruidDataSourceUtil;
import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean; import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean;
import com.zyplayer.doc.db.framework.db.interceptor.SqlLogInterceptor; import com.zyplayer.doc.db.framework.db.interceptor.SqlLogInterceptor;
import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Interceptor;
@@ -18,33 +17,7 @@ public class DatasourceUtil {
public static DatabaseFactoryBean createDatabaseFactoryBean(DbDatasource dbDatasource){ public static DatabaseFactoryBean createDatabaseFactoryBean(DbDatasource dbDatasource){
try { try {
// 数据源配置 // 数据源配置
DruidDataSource dataSource = new DruidDataSource(); DruidDataSource dataSource = DruidDataSourceUtil.createDataSource(dbDatasource.getDriverClassName(), dbDatasource.getSourceUrl(), dbDatasource.getSourceName(), dbDatasource.getSourcePassword());
dataSource.setDriverClassName(dbDatasource.getDriverClassName());
dataSource.setUrl(dbDatasource.getSourceUrl());
dataSource.setUsername(dbDatasource.getSourceName());
dataSource.setPassword(dbDatasource.getSourcePassword());
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(50);
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setTestOnReturn(false);
dataSource.setValidationQuery("select 1");
dataSource.setMaxWait(3000);
dataSource.setTimeBetweenEvictionRunsMillis(60000);
dataSource.setMinEvictableIdleTimeMillis(3600000);
// 重试3次失败退出源码里是errorCount > connectionErrorRetryAttempts所以写成2就是3次、、、
// CreateConnectionThread 源码在这个类里面
dataSource.setConnectionErrorRetryAttempts(2);
dataSource.setBreakAfterAcquireFailure(true);
dataSource.setName("zyplayer-doc-db-" + dbDatasource.getId());
if (dbDatasource.getSourceUrl().contains("oracle")) {
dataSource.setValidationQuery("select 1 from dual");
}
DruidPooledConnection connection = dataSource.getConnection(3000);
if (connection == null) {
throw new ConfirmException("尝试获取该数据源连接失败:" + dbDatasource.getSourceUrl());
}
// 描述连接信息的对象 // 描述连接信息的对象
DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean(); DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean();
Resource[] resources = null; Resource[] resources = null;
@@ -86,7 +59,6 @@ public class DatasourceUtil {
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.ORACLE); databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.ORACLE);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/oracle/*.xml"); resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/oracle/*.xml");
} }
connection.recycle();
if (resources == null) { if (resources == null) {
return null; return null;
} }

View File

@@ -40,10 +40,6 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<!-- 在线文档解析页面 --> <!-- 在线文档解析页面 -->
<!--zyplayer-doc-core--> <!--zyplayer-doc-core-->
<dependency> <dependency>

View File

@@ -30,9 +30,6 @@
</encoder> </encoder>
</appender> </appender>
<logger name="com.zyplayer.doc.manage.repository" level="warn" /> <logger name="com.zyplayer.doc.manage.repository" level="warn" />
<logger name="com.atomikos.jdbc" level="warn" />
<logger name="com.atomikos.datasource.xa" level="warn" />
<logger name="com.atomikos.icatch.imp" level="warn" />
<root level="info"> <root level="info">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>

View File

@@ -30,9 +30,6 @@
</encoder> </encoder>
</appender> </appender>
<logger name="com.zyplayer.doc.manage.repository" level="warn" /> <logger name="com.zyplayer.doc.manage.repository" level="warn" />
<logger name="com.atomikos.jdbc" level="warn" />
<logger name="com.atomikos.datasource.xa" level="warn" />
<logger name="com.atomikos.icatch.imp" level="warn" />
<root level="info"> <root level="info">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>