- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以在一个应用程序中同时使用这两个启动器?
我想将 CSV 文件中的记录加载到数据库表中。 Spring Batch 表存储在不同的数据库中,因此我假设我需要使用 JTA 来处理事务。
每当我将 @EnableBatchProcessing 添加到我的 @Configuration 类时,它都会配置一个 PlatformTransactionManager,这会阻止 Atomikos 自动配置它。
是否有任何 spring boot + batch + jta 示例展示了如何执行此操作?
非常感谢,詹姆斯
最佳答案
我刚刚经历了这个,我发现了一些似乎有效的东西。正如您所注意到的,@EnableBatchProcessing
会导致创建一个 DataSourceTransactionManager
,这会弄乱一切。我在 @EnableBatchProcessing
中使用 modular=true,所以 ModularBatchConfiguration
类被激活。
我所做的是停止使用 @EnableBatchProcessing
,而是将整个 ModularBatchConfiguration
类复制到我的项目中。然后我注释掉了 transactionManager()
方法,因为 Atomikos 配置创建了 JtaTransactionManager
。我还必须覆盖 jobRepository()
方法,因为它被硬编码为使用在 DefaultBatchConfiguration
中创建的 DataSourceTransactionManager
。
我还必须显式导入 JtaAutoConfiguration
类。这会正确连接所有内容(根据 Actuator 的“beans”端点 - 感谢上帝)。但是当你运行它时,事务管理器会抛出一个异常,因为某处某处设置了一个显式的事务隔离级别。所以我也写了一个BeanPostProcesso
r来找到事务管理器并调用txnMgr.setAllowCustomIsolationLevels(true)
;
现在一切正常,但是当作业正在运行时,我无法使用 JdbcTemplate
从 batch_step_execution 表中获取当前数据,即使我可以在 SQLYog 中看到数据。这一定和事务隔离有关,但我还没有弄明白。
这是我的配置类,从 Spring 复制并按上述修改。 PS,我有我的 DataSource
,它指向批处理表注释为 @Primary
的数据库。此外,我将 DataSource
bean 更改为 org.apache.tomcat.jdbc.pool.XADataSource
的实例;我不确定是否有必要。
@Configuration
@Import(ScopeConfiguration.class)
public class ModularJtaBatchConfiguration implements ImportAware
{
@Autowired(required = false)
private Collection<DataSource> dataSources;
private BatchConfigurer configurer;
@Autowired
private ApplicationContext context;
@Autowired(required = false)
private Collection<BatchConfigurer> configurers;
private AutomaticJobRegistrar registrar = new AutomaticJobRegistrar();
@Bean
public JobRepository jobRepository(DataSource batchDataSource, JtaTransactionManager jtaTransactionManager) throws Exception
{
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(batchDataSource);
factory.setTransactionManager(jtaTransactionManager);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public JobLauncher jobLauncher() throws Exception {
return getConfigurer(configurers).getJobLauncher();
}
// @Bean
// public PlatformTransactionManager transactionManager() throws Exception {
// return getConfigurer(configurers).getTransactionManager();
// }
@Bean
public JobExplorer jobExplorer() throws Exception {
return getConfigurer(configurers).getJobExplorer();
}
@Bean
public AutomaticJobRegistrar jobRegistrar() throws Exception {
registrar.setJobLoader(new DefaultJobLoader(jobRegistry()));
for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) {
registrar.addApplicationContextFactory(factory);
}
return registrar;
}
@Bean
public JobBuilderFactory jobBuilders(JobRepository jobRepository) throws Exception {
return new JobBuilderFactory(jobRepository);
}
@Bean
// hopefully this will autowire the Atomikos JTA txn manager
public StepBuilderFactory stepBuilders(JobRepository jobRepository, JtaTransactionManager ptm) throws Exception {
return new StepBuilderFactory(jobRepository, ptm);
}
@Bean
public JobRegistry jobRegistry() throws Exception {
return new MapJobRegistry();
}
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
AnnotationAttributes enabled = AnnotationAttributes.fromMap(importMetadata.getAnnotationAttributes(
EnableBatchProcessing.class.getName(), false));
Assert.notNull(enabled,
"@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName());
}
protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers) throws Exception {
if (this.configurer != null) {
return this.configurer;
}
if (configurers == null || configurers.isEmpty()) {
if (dataSources == null || dataSources.isEmpty()) {
throw new UnsupportedOperationException("You are screwed");
} else if(dataSources != null && dataSources.size() == 1) {
DataSource dataSource = dataSources.iterator().next();
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(dataSource);
configurer.initialize();
this.configurer = configurer;
return configurer;
} else {
throw new IllegalStateException("To use the default BatchConfigurer the context must contain no more than" +
"one DataSource, found " + dataSources.size());
}
}
if (configurers.size() > 1) {
throw new IllegalStateException(
"To use a custom BatchConfigurer the context must contain precisely one, found "
+ configurers.size());
}
this.configurer = configurers.iterator().next();
return this.configurer;
}
}
@Configuration
class ScopeConfiguration {
private StepScope stepScope = new StepScope();
private JobScope jobScope = new JobScope();
@Bean
public StepScope stepScope() {
stepScope.setAutoProxy(false);
return stepScope;
}
@Bean
public JobScope jobScope() {
jobScope.setAutoProxy(false);
return jobScope;
}
}
关于spring-boot-starter-jta-atomikos 和 spring-boot-starter-batch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921177/
如果我设置 com.atomikos.icatch.enable_logging=false ,我想了解分布式事务功能是否适用于我的应用程序 我是否理解正确,事务恢复与发生崩溃的情况相关,我们希望完全
我们的项目依靠 Atomikos 提供轻量级的交易管理。但是,我们发现它在初始化过程中以明文形式记录了数据库用户名和密码。 例如 2015-10-15 16:43:01,106 [http-bio-8
我正在试验 Atomikos 提供的独立 JPA 和 JTA/XA 事务管理。 我的简单单元测试保留了 3 条记录,包装在 JTA UserTransaction 中。 当使用H2作为后备数据库时,测
我继承了一个应用程序,该应用程序使用 Atomikos 在 Oracle 数据库之上的 Spring 中进行事务处理。在生产部署中,始终通过设置 com.atomikos.icatch.enable_
除了我们遵循文档并将日志记录目录和基本名称指定为 -D 参数外,我们看到的与 Strange Atomikos exception - Error in init(): Log already in
Atomikos 在使用时非常冗长。事务管理器向控制台写入了很多 INFO 消息(大部分与我无关)。 transaction.properties 中的设置应该控制消息级别 com.atomikos.
当通过 Spring 配置 Atomikos 时,不需要 jta.properties 或 transactions.properties 文件。尽管如此,Atomikos 启动时会将以下消息打印到
正如线程标题中所述,我们使用 Spring 框架、带有 hibernate 提供程序的 JPA 和 Atomikos 作为事务提供程序。 大多数情况下,此设置都有效,但在 Tomcat 启动过程中出现
当通过 Spring 配置 Atomikos 时,不需要 jta.properties 或 transactions.properties 文件。尽管如此,Atomikos 启动时会将以下消息打印到
我有一个问题,我必须在 Spring-Boot 中使用 Atomikos 和 Axon 框架(没有 Axon 服务器)。我正在使用 Oracle DB,并且使用多个线程 (10) 发送大量命令,在此之
我正在尝试将 EHCache 的事务处理能力与 Atomikos 和 Apache Tomcat 一起使用(与 JMS 和 Hibernate 一起使用,但这只是为了向怀疑论者保证我真的需要 JTA)
我的应用程序(在 Tomcat 服务器上运行)使用 atomikos 连接池连接 mysql 数据库。一切正常,除了连接会如果离开应用程序服务器几个小时不使用,将被关闭。下面是发生这种情况后再次运行应
我们有一个应用程序在 Java 7 上运行良好几年,但在升级到 Java 8 时遇到问题。数据库连接似乎是问题所在。 情况是这样的:该应用程序访问 200 多个数据库。或者,从技术上讲,一个数据库安装
我在使用 Atomikos 时遇到了一个奇怪的问题。 我有一个小型测试应用程序(Spring + Hibernate)。它使用两个不同的数据源,我需要在非 Java EE 容器(在我的例子中是 Tom
我们想使用 Atomikos JTA 事务管理器。我们有一个单元测试,我们希望在它完成后回滚,从而使表保持干净以备下次运行。 @RunWith(SpringJUnit4ClassRunner.clas
我正在开发一个具有多个 JDBC 数据源和 JTA 的项目。我使用 Maven 作为构建工具,并且我想使用 Jetty 插件 (6.1.20) 在开发过程中运行应用程序。 我正在尝试将 Jetty 配
我有一个监听 ActiveMQ 的 Camel 路由。我添加了 10 秒的延迟,因为在开始之前需要确定另一个进程已完成。这是通过添加延迟器属性来实现的:- ....extra st
我正在创建一个示例应用程序来测试两阶段提交 (2PC)。我从互联网上获取了此处使用的代码位。我使用 Spring、Hibernate 和 Atomikos,并以 MySQL 作为后端。我正在使用两个数
我尝试在不使用 spring 的情况下配置 Atomikos Transaction。首先,我尝试在不使用 spring 的情况下设置 EntityManagerFactory,以下是我尝试过的代码
我想将 atomikos 日志级别设置为高于 INFO。 Tomcat 的 catalina.out 日志文件写入许多 INFO 消息,导致事务需要很长时间才能完成。 在启动期间,catalina.o
我是一名优秀的程序员,十分优秀!