apache / apache/shardingsphere

An error occurs when using XA transactions:java.lang.NullPointerException

Open
#26,759 0 comments 0 reactions 0 assignees View on GitHub
feature: transaction
Dominant language
Java
Stars
20.8k
Forks
6.9k
Avg merge
11h 35m
Merged PRs (30d)
326

Description

**version:5.3.2**

**Debug:**

After the execution of the init method in XAShardingSphereTransactionManager, the subsequent execution will involve the close method. Inside the close method, the cachedDataSources.clear() operation will be performed.

init, note the object ID @8060
![image](https://github.com/apache/shardingsphere/assets/4153607/deac4f21-6b0b-4cd3-a77c-608458c62c2b)

In the close method, the cachedDataSources of the object with ID @8060 is cleared.
![image](https://github.com/apache/shardingsphere/assets/4153607/501fb10a-71eb-441d-9239-5d48566c4352)

When invoking the getConnection method at this point, the cachedDataSources is empty.
![image](https://github.com/apache/shardingsphere/assets/4153607/64543e1d-b911-48d3-bc09-c9b47d637c6a)

Then an error occurs.
![image](https://github.com/apache/shardingsphere/assets/4153607/9d1a04f5-8d86-4d15-9fe3-e5ce46ebf7ae)

The ZookeeperRepository listens for configuration changes and then triggers ShardingSphereTransactionManagerEngine.close.
![image](https://github.com/apache/shardingsphere/assets/4153607/c099bd5b-b4d6-49c6-9f89-e5379906e57e)

If I comment out the watch code, the program can run normally without any errors.
![image](https://github.com/apache/shardingsphere/assets/4153607/96326713-166b-406d-88ae-5b291432cf9f)

**stack trace:**
`Caused by: java.lang.NullPointerException
at org.apache.shardingsphere.transaction.xa.XAShardingSphereTransactionManager.getConnection(XAShardingSphereTransactionManager.java:91)
at org.apache.shardingsphere.transaction.ConnectionTransaction.getConnection(ConnectionTransaction.java:106)
at org.apache.shardingsphere.driver.jdbc.core.connection.ConnectionManager.createConnection(ConnectionManager.java:365)
at org.apache.shardingsphere.driver.jdbc.core.connection.ConnectionManager.createConnections(ConnectionManager.java:334)
at org.apache.shardingsphere.driver.jdbc.core.connection.ConnectionManager.getConnections(ConnectionManager.java:323)
at org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine.group(DriverExecutionPrepareEngine.java:88)
at org.apache.shardingsphere.infra.executor.sql.prepare.AbstractExecutionPrepareEngine.prepare(AbstractExecutionPrepareEngine.java:65)
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.createExecutionGroupContext(ShardingSpherePreparedStatement.java:533)
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.useDriverToExecute(ShardingSpherePreparedStatement.java:508)
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.execute(ShardingSpherePreparedStatement.java:443)`

Contributor guide

Open the contributing guide

Research direction

Start at XAShardingSphereTransactionManager.getConnection and its init and close methods, using the stack trace location at line 91. Then trace how ZookeeperRepository configuration watches trigger ShardingSphereTransactionManagerEngine.close. Done means the XA transaction path no longer raises the reported NullPointerException after a configuration change; the payload does not name a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
databases, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.