使用了druid + atomikos,使用的时候可以正常夸库事物操作,但是启动IDE的时候会出现一堆atomikos transaction manager not running的错误,查原因说是启动顺序的问题,加一个标注就能解决。
JAT事物类
package com.java.core.mapper.config;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
/**
* JTA 事务配置
*/
@Configuration
public class AtomikosConfig {
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(10000);
return userTransactionImp;
}
@Bean(name = "atomikosTransactionManager")
public TransactionManager atomikosTransactionManager() throws Throwable {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
return userTransactionManager;
}
@Bean(name = "transactionManager")
@DependsOn({ "userTransaction", "atomikosTransactionManager" })
public PlatformTransactionManager transactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
TransactionManager atomikosTransactionManager = atomikosTransactionManager();
return new JtaTransactionManager(userTransaction, atomikosTransactionManager);
}
}
在多库中加标注:
@DependsOn({"transactionManager"})
@Bean(name = "masterDataSource")
@Primary
@DependsOn({"transactionManager"})
public DataSource masterDataSource(@Qualifier("masterXADataSource") DruidXADataSource masterXADataSource) {
//将本地事务注册到Atomikos全局事务
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(masterXADataSource);
xaDataSource.setUniqueResourceName("master");
return xaDataSource;
}
在启动的时候提示就正常了
2022-11-22 16:50:39.561 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.default_max_wait_time_on_shutdown = 9223372036854775807
2022-11-22 16:50:39.561 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.allow_subtransactions = true
2022-11-22 16:50:39.561 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.recovery_delay = 10000
2022-11-22 16:50:39.561 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.automatic_resource_registration = true
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.oltp_max_retries = 5
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.client_demarcation = false
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.threaded_2pc = false
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.serial_jta_transactions = true
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.log_base_dir = ./
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.rmi_export_class = none
2022-11-22 16:50:39.562 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.max_actives = 50
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.checkpoint_interval = 500
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.enable_logging = true
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.log_base_name = tmlog
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.max_timeout = 300000
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.trust_client_tm = false
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: java.naming.factory.initial = com.sun.jndi.rmi.registry.RegistryContextFactory
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.tm_unique_name = 192.168.146.1.tm
2022-11-22 16:50:39.563 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.forget_orphaned_log_entries_delay = 86400000
2022-11-22 16:50:39.564 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.oltp_retry_interval = 10000
2022-11-22 16:50:39.564 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: java.naming.provider.url = rmi://localhost:1099
2022-11-22 16:50:39.564 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.force_shutdown_on_vm_exit = false
2022-11-22 16:50:39.564 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - USING: com.atomikos.icatch.default_jta_timeout = 10000
2022-11-22 16:50:39.566 [main] INFO com.atomikos.icatch.provider.imp.AssemblerImp - Using default (local) logging and recovery...
2022-11-22 16:50:44.394 [main] INFO s.d.s.w.PropertySourcedRequestMappingHandlerMapping - Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2022-11-22 16:50:46.198 [main] INFO o.s.security.web.DefaultSecurityFilterChain - Will not secure any request
2022-11-22 16:50:46.403 [main] WARN org.mybatis.spring.SqlSessionFactoryBean - Property 'mapperLocations' was specified but matching resources are not found.
2022-11-22 16:50:48.535 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
2022-11-22 16:50:48.572 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
2022-11-22 16:50:48.575 [main] INFO s.d.s.web.plugins.DocumentationPluginsBootstrapper - Context refreshed
2022-11-22 16:50:48.623 [main] INFO s.d.s.web.plugins.DocumentationPluginsBootstrapper - Found 1 custom documentation plugin(s)
2022-11-22 16:50:48.753 [main] INFO s.d.spring.web.scanners.ApiListingReferenceScanner - Scanning for api listing references
2022-11-22 16:50:49.089 [main] INFO s.d.s.w.r.operation.CachingOperationNameGenerator - Generating unique operation named: delOrgUsingPOST_1
2022-11-22 16:50:49.210 [main] INFO com.java.core.web.Application - Started Application in 19.603 seconds (JVM running for 24.311)