springboot+jpa+jta-atomikos多数据源事务异常 100C

springboot + jpa配置jta事务,出现第一个数据源可以正常使用,但第二个只能查询,不能CRUD操作,求大神指点

第一个数据源如下:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.saxon.primary.dao",
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "jtaTransactionManager")
public class PrimaryDataSourceConfig {

@Bean
@Primary
@ConfigurationProperties("spring.primary.datasource")
public DataSourceProperties primaryProperties(){
    return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("spring.primary.datasource")
public DataSource primaryDataSource(){
    return primaryProperties().initializeDataSourceBuilder().build();
}

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder){
    return builder
            .dataSource(primaryDataSource())
            .persistenceUnit("primary")
            .packages("com.saxon.primary.entity")
            .build();
}

}

第二个数据源:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.saxon.others.dao",
entityManagerFactoryRef = "othersEntityManagerFactory",
transactionManagerRef = "jtaTransactionManager")
public class OthersDataSourceConfig {

@Bean
@ConfigurationProperties("spring.others.datasource")
public DataSourceProperties othersProperties(){
    return new DataSourceProperties();
}

@Bean
@ConfigurationProperties("spring.others.datasource")
public DataSource othersDataSource(){
    return othersProperties().initializeDataSourceBuilder().build();
}

@Bean
public LocalContainerEntityManagerFactoryBean othersEntityManagerFactory(EntityManagerFactoryBuilder builder){
    return builder
            .dataSource(othersDataSource())
            .persistenceUnit("others")
            .packages("com.saxon.others.entity")
            .build();
}

}

jta事务配置:
@Configuration
@EnableTransactionManagement
public class TransactionManagerConfig {

@Bean(destroyMethod = "close", initMethod = "init")
public UserTransactionManager userTransactionManager() {
    UserTransactionManager userTransactionManager = new UserTransactionManager();
    userTransactionManager.setForceShutdown(false);
    return userTransactionManager;
}

public @Bean UserTransaction userTransaction() throws Throwable {
    UserTransactionImp userTransactionImp = new UserTransactionImp();
    userTransactionImp.setTransactionTimeout(300);
    return userTransactionImp;
}

public @Bean PlatformTransactionManager jtaTransactionManager() throws Throwable  {
    JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
    jtaTransactionManager.setTransactionManager(userTransactionManager());
    jtaTransactionManager.setUserTransaction(userTransaction());
    return jtaTransactionManager;
}

}
但执行第二个数据源的crud操作,报如下异常:
2017-07-20 18:00:43.131 ERROR 46084 --- [nio-9008-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress] with root cause

javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1136) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1297) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at com.sun.proxy.$Proxy76.flush(Unknown Source) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]

MrCoderStack
咸鱼君0808 解决了没?
一年多之前 回复

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐