Skip to content

Commit

Permalink
optimize: remove the branch registration operation of the XA read-onl…
Browse files Browse the repository at this point in the history
…y transaction (#6826)
  • Loading branch information
GoodBoyCoder authored Sep 21, 2024
1 parent 0562e9f commit f5b897e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Add changes here for all PR submitted to the 2.x branch.


### optimize:
- [[#6826](https://github.com/apache/incubator-seata/pull/6826)] remove the branch registration operation of the XA read-only transaction


### refactor:

Expand All @@ -20,6 +22,7 @@ Thanks to these contributors for their code commits. Please report an unintended

<!-- Please make sure your Github ID is in the list below -->
- [slievrly](https://github.com/slievrly)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
4 changes: 3 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@


### optimize:
- [[#6826](https://github.com/apache/incubator-seata/pull/6826)] 移除只读XA事务的分支注册操作



### refactor:
Expand All @@ -22,7 +24,7 @@

<!-- 请确保您的 GitHub ID 在以下列表中 -->
- [slievrly](https://github.com/slievrly)

- [GoodBoyCoder](https://github.com/GoodBoyCoder)


同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
if (currentAutoCommitStatus == autoCommit) {
return;
}
if (isReadOnly()) {
//If it is a read-only transaction, do nothing
currentAutoCommitStatus = autoCommit;
return;
}
if (autoCommit) {
// According to JDBC spec:
// If this method is called during a transaction and the
Expand Down Expand Up @@ -210,8 +215,8 @@ public boolean getAutoCommit() throws SQLException {

@Override
public synchronized void commit() throws SQLException {
if (currentAutoCommitStatus) {
// Ignore the committing on an autocommit session.
if (currentAutoCommitStatus || isReadOnly()) {
// Ignore the committing on an autocommit session and read-only transaction.
return;
}
if (!xaActive || this.xaBranchXid == null) {
Expand Down Expand Up @@ -251,8 +256,8 @@ public synchronized void commit() throws SQLException {

@Override
public void rollback() throws SQLException {
if (currentAutoCommitStatus) {
// Ignore the committing on an autocommit session.
if (currentAutoCommitStatus || isReadOnly()) {
// Ignore the committing on an autocommit session and read-only transaction.
return;
}
if (!xaActive || this.xaBranchXid == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,40 @@ public void testCreateStatement() throws Throwable {
Assertions.assertTrue(statement instanceof StatementProxyXA);
}

@Test
public void testXAReadOnly() throws Throwable {
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
Mockito.when(connection.isReadOnly()).thenReturn(true);

XAResource xaResource = Mockito.mock(XAResource.class);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
Mockito.when(xaConnection.getXAResource()).thenReturn(xaResource);
BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
String xid = "xxx";
ResourceManager resourceManager = Mockito.mock(ResourceManager.class);
Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class));
DefaultResourceManager.get();
DefaultResourceManager.mockResourceManager(BranchType.XA, resourceManager);

ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
connectionProxyXA.init();
connectionProxyXA.setAutoCommit(false);

// Assert setAutoCommit = false was NEVER invoked on the wrapped connection
Mockito.verify(connection, times(0)).setAutoCommit(false);
// Assert XA start was invoked
Mockito.verify(xaResource, times(0)).start(any(Xid.class), any(Integer.class));

connectionProxyXA.commit();

Mockito.verify(xaResource, times(0)).end(any(Xid.class), any(Integer.class));
Mockito.verify(xaResource, times(0)).prepare(any(Xid.class));

connectionProxyXA.rollback();
Mockito.verify(xaResource, times(0)).rollback(any(Xid.class));
}

@AfterAll
public static void tearDown(){
RootContext.unbind();
Expand Down

0 comments on commit f5b897e

Please sign in to comment.