Skip to content

Commit 4134e8e

Browse files
committed
opt
1 parent dd9ef73 commit 4134e8e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

rm-datasource/src/main/java/io/seata/rm/datasource/xa/DataSourceProxyXA.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ public DataSourceProxyXA(DataSource dataSource, String resourceGroupId) {
5656
this.branchType = BranchType.XA;
5757
JdbcUtils.initDataSourceResource(this, dataSource, resourceGroupId);
5858
if (DBType.MYSQL.name().equalsIgnoreCase(dbType)) {
59-
Optional.ofNullable(DefaultResourceManager.get().getResourceManager(BranchType.XA)).ifPresent(resourceManager -> {
60-
if (resourceManager instanceof ResourceManagerXA) {
61-
((ResourceManagerXA)resourceManager).initXaTwoPhaseTimeoutChecker();
62-
}
63-
});
6459
try (Connection connection = dataSource.getConnection();
6560
PreparedStatement preparedStatement = connection.prepareStatement("SELECT VERSION()");
6661
ResultSet versionResult = preparedStatement.executeQuery()) {
@@ -78,6 +73,11 @@ public DataSourceProxyXA(DataSource dataSource, String resourceGroupId) {
7873
} else if (DBType.MARIADB.name().equalsIgnoreCase(dbType)) {
7974
setShouldBeHeld(true);
8075
}
76+
Optional.ofNullable(DefaultResourceManager.get().getResourceManager(BranchType.XA)).ifPresent(resourceManager -> {
77+
if (resourceManager instanceof ResourceManagerXA) {
78+
((ResourceManagerXA)resourceManager).initXaTwoPhaseTimeoutChecker();
79+
}
80+
});
8181
//Set the default branch type to 'XA' in the RootContext.
8282
RootContext.setDefaultBranchType(this.getBranchType());
8383
}

rm-datasource/src/main/java/io/seata/rm/datasource/xa/ResourceManagerXA.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,20 @@ public void initXaTwoPhaseTimeoutChecker() {
7575
xaTwoPhaseTimeoutChecker.scheduleAtFixedRate(() -> {
7676
for (Map.Entry<String, Resource> entry : dataSourceCache.entrySet()) {
7777
BaseDataSourceResource resource = (BaseDataSourceResource)entry.getValue();
78-
if (resource instanceof DataSourceProxyXA) {
79-
Map<String, ConnectionProxyXA> keeper = resource.getKeeper();
80-
for (Map.Entry<String, ConnectionProxyXA> connectionEntry : keeper.entrySet()) {
81-
ConnectionProxyXA connection = connectionEntry.getValue();
82-
long now = System.currentTimeMillis();
83-
synchronized (connection) {
84-
if (connection.getPrepareTime() != null
85-
&& now - connection.getPrepareTime() > TWO_PHASE_HOLD_TIMEOUT) {
86-
try {
87-
connection.closeForce();
88-
} catch (SQLException e) {
89-
LOGGER.info("Force close the xa physical connection fail", e);
78+
if (resource.isShouldBeHeld()) {
79+
if (resource instanceof DataSourceProxyXA) {
80+
Map<String, ConnectionProxyXA> keeper = resource.getKeeper();
81+
for (Map.Entry<String, ConnectionProxyXA> connectionEntry : keeper.entrySet()) {
82+
ConnectionProxyXA connection = connectionEntry.getValue();
83+
long now = System.currentTimeMillis();
84+
synchronized (connection) {
85+
if (connection.getPrepareTime() != null
86+
&& now - connection.getPrepareTime() > TWO_PHASE_HOLD_TIMEOUT) {
87+
try {
88+
connection.closeForce();
89+
} catch (SQLException e) {
90+
LOGGER.info("Force close the xa physical connection fail", e);
91+
}
9092
}
9193
}
9294
}

0 commit comments

Comments
 (0)