Shardingsphere: Transactions cannot be rolled back properly when using local transaction.

Created on 17 Jan 2020  ·  10Comments  ·  Source: apache/shardingsphere

Bug Report

For English only, other languages will not accept.

Before report a bug, make sure you have:

Please pay attention on issues you submitted, because we maybe need more details.
If no response more than 7 days and we cannot reproduce it on current information, we will close it.

Please answer these questions before submitting your issue. Thanks!

Which version of ShardingSphere did you use?

4.0.0-RC2

Which project did you use? Sharding-JDBC or Sharding-Proxy?

Sharding-JDBC

Expected behavior

https://shardingsphere.apache.org/document/current/en/features/transaction/function/local-transaction/
image

Actual behavior

1, The insert is committed directly.
2, Local transaction cannot be rolled back after a null pointer exception is thrown.

Example code:
image

Reason analyze (If you can)

Distributed transactions are enabled after the sharding-transaction-xa-core package is imported into the project.
The reason for the insert is directly committed is probably because the autoCommit property of the ShardingConnection class defaults to true and then returns false in the isHoldTransaction function of the ShardingConnection class.

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

Please see the sample code below.

Example codes for reproduce this issue (such as a github link).

https://github.com/Zze0/demo-sharding-jdbc

transaction question

Most helpful comment

this is a constraint in spring side, you can try to PR for it to become our new contributor

Ok, I will

All 10 comments

hi, you should exclude the JtaAutoConfiguration in your project like this

@SpringBootApplication(exclude = JtaAutoConfiguration.class)

aslo @ShardingTransactionType doesn't work without sharding-transaction-jdbc-spring dependency. we haven't merge it to ShardingSphere now, you should deploy it by yourself

hi, you should exclude the JtaAutoConfiguration in your project like this

@SpringBootApplication(exclude = JtaAutoConfiguration.class)

aslo @ShardingTransactionType doesn't work without sharding-transaction-jdbc-spring dependency. we haven't merge it to ShardingSphere now, you should deploy it by yourself

Thank you for your patient reply.
It finally worked when I excluded JtaAutoConfiguration from my project.

The solution is to use DataSourceTransactionManager by default after excluded JtaAutoConfiguration ?

The second question is, can I use 2PC XA transactions after excluding JtaAutoConfiguration? (Even if that means no JtaTransactionManager)

hi, you should exclude the JtaAutoConfiguration in your project like this

@SpringBootApplication(exclude = JtaAutoConfiguration.class)

aslo @ShardingTransactionType doesn't work without sharding-transaction-jdbc-spring dependency. we haven't merge it to ShardingSphere now, you should deploy it by yourself

As for your statement that "@ShardingTransactionType doesn't work without sharding-transaction-jdbc-spring dependency", which version will be merged?

Currently, do we have to develop the @ShardingTransactionType aspect by ourselves?

Like this? https://github.com/OpenSharding/shardingsphere-spi-impl/blob/4.0.0-RC2/sharding-spring/sharding-transaction-spring/sharding-transaction-jdbc-spring/src/main/java/io/shardingsphere/transaction/aspect/ShardingTransactionJDBCAspect.java

After debugging, I found that excluding or not excluding JtaAutoConfiguration made a big difference.


Don’t exclude JtaAutoConfiguration, use JtaTransactionManager.

When commit:
1、org.springframework.transaction.support.AbstractPlatformTransactionManager#commit
2、org.springframework.transaction.jta.JtaTransactionManager#doCommit
3、com.atomikos.icatch.jta.UserTransactionManager#commit


Exclude JtaAutoConfiguration, use DataSourceTransactionManager.

When commit:
1、org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection#commit
2、org.apache.shardingsphere.transaction.xa.XAShardingTransactionManager#commit
3、com.atomikos.icatch.jta.UserTransactionManager#commit


Does this mean that JtaAutoConfiguration must be excluded to ensure that sharding-jdbc controls the commit or rollback ? @cherrylzhao

Wish you a happy Spring Festival in advance !

Can someone solve my doubts?
It’s very important for me.
I still have a production problem to solve.
IMG20200119-110527

@KomachiSion @terrymanu @cherrylzhao

After debugging, I found that excluding or not excluding JtaAutoConfiguration made a big difference.

Don’t exclude JtaAutoConfiguration, use JtaTransactionManager.

When commit:
1、org.springframework.transaction.support.AbstractPlatformTransactionManager#commit
2、org.springframework.transaction.jta.JtaTransactionManager#doCommit
3、com.atomikos.icatch.jta.UserTransactionManager#commit

Exclude JtaAutoConfiguration, use DataSourceTransactionManager.

When commit:
1、org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection#commit
2、org.apache.shardingsphere.transaction.xa.XAShardingTransactionManager#commit
3、com.atomikos.icatch.jta.UserTransactionManager#commit

Does this mean that JtaAutoConfiguration must be excluded to ensure that sharding-jdbc controls the commit or rollback ? @cherrylzhao

If you want to use ShardingSphere XA, you need to exclude JtaTransactionManager. Because JtaTransactionManager will shutdown the Transaction control of ShardingSphere.

If use local or base of ShardingSphere, JtaTransactionManager can be excluded or not.

Currently, do we have to develop the @ShardingTransactionType aspect by ourselves?

There is no need to develop @ShardingTransactionType by yourself. But only you should clone the shardingsphere-spi-impl repo and install by yourself.

ShardingSphere use local transaction interface to integrate distributed transaction(XA and BASE).
from the spring perspective we only compatible with DataSourceTransactionManager,so JTA
TransactionMananger will conflict with us. @ShardingTransactionType is a customer annotation, it should be handled by aspect class, you can use our code of SPI project or develop by yourself.

@KomachiSion @cherrylzhao
Thank you very much for your answer.
My problem is solved.


If you want to use ShardingSphere XA, you need to exclude JtaTransactionManager. Because JtaTransactionManager will shutdown the Transaction control of ShardingSphere.

In addition, I think you need to update your official documents on this point.
Right here: https://shardingsphere.apache.org/document/current/en/manual/sharding-jdbc/usage/transaction/

this is a constraint in spring side, you can try to PR for it to become our new contributor

this is a constraint in spring side, you can try to PR for it to become our new contributor

Ok, I will

Was this page helpful?
0 / 5 - 0 ratings