liuyangming / ByteTCC

ByteTCC is a distributed transaction manager based on the TCC(Try/Confirm/Cancel) mechanism. It’s compatible with the JTA specification. User guide: https://github.com/liuyangming/ByteTCC/wiki
https://www.bytesoft.org/
GNU Lesser General Public License v3.0
2.9k stars 911 forks source link

spring-jpa 整合 #124

Open TarryHoo opened 4 years ago

TarryHoo commented 4 years ago

@liuyangming 大佬,有个问题请教下.

最近在做springcloud 和 Jpa 整合. 请求后报错

Caused by: java.sql.SQLException: Illegal operation!
    at org.bytesoft.bytejta.supports.jdbc.LogicalConnection.setAutoCommit(LogicalConnection.java:83) ~[bytejta-core-0.5.0-BETA8.jar:na]
    at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
    ... 204 common frames omitted

配置如下

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties.hibernate.format_sql: true
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect  
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: org.apache.commons.dbcp2.BasicDataSource

版本如下

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.3.10.Final</version>
</dependency>

报错的代码是AbstractLogicalConnectionImplementor.begin()

@Override
public void begin() {
    try {
        if ( !doConnectionsFromProviderHaveAutoCommitDisabled() ) {
            log.trace( "Preparing to begin transaction via JDBC Connection.setAutoCommit(false)" );
            getConnectionForTransactionManagement().setAutoCommit( false );
            log.trace( "Transaction begun via JDBC Connection.setAutoCommit(false)" );
        }
        status = TransactionStatus.ACTIVE;
    }
    catch( SQLException e ) {
        throw new TransactionException( "JDBC begin transaction failed: ", e );
    }
}
liuyangming commented 4 years ago

你这个事务走的不是byteTCC的事务管理器,可能走了HibernateTransactionManager。byteTCC针对jpa的集成,请参考ByteTCC-sample/springboot-sample/sample-provider,具体配置看一下这几个类: 1、SpringBootProviderBootstrap: 留意一下@EnableJpaRepositories注解的配置; 2、ProviderConfig: 留意LocalContainerEntityManagerFactoryBean的配置; 3、META-INF/persistence.xml;

TarryHoo commented 4 years ago

你这个事务走的不是byteTCC的事务管理器,可能走了HibernateTransactionManager。byteTCC针对jpa的集成,请参考ByteTCC-sample/springboot-sample/sample-provider,具体配置看一下这几个类: 1、SpringBootProviderBootstrap: 留意一下@EnableJpaRepositories注解的配置; 2、ProviderConfig: 留意LocalContainerEntityManagerFactoryBean的配置; 3、META-INF/persistence.xml;

好的,多谢大佬解答.我去看下

TarryHoo commented 4 years ago

@liuyangming 大佬 昨晚测试了下, 不支持springboot 2.1.X 2.1.X的springboot DataSourceInitializedPublisher 做了些改动. 如下是我整合到2.1.X sb 的修改 image

liuyangming commented 4 years ago

好的,感谢反馈。