Skip to content

bugfix: SQLIntegrityConstraintViolationException capture incorrectly when inserting a globallock #6833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
*/
package org.apache.seata.server.storage.db.lock;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -372,10 +368,27 @@ protected boolean doAcquireLocks(Connection conn, List<LockDO> lockDOs) throws S
return ps.executeBatch().length == lockDOs.size();
} catch (SQLIntegrityConstraintViolationException e) {
LOGGER.error("Global lock batch acquire error: {}", e.getMessage(), e);
//return false,let the caller go to conn.rollabck()
//return false,let the caller go to conn.rollback()
return false;
} catch (BatchUpdateException e) {
Throwable cause = e.getCause();
if (cause != null) {
if (cause instanceof SQLIntegrityConstraintViolationException) {
return false;
}
throw e;
}
SQLException nextException = e.getNextException();
if (nextException == null) {
throw e;
}
while (nextException != null) {
if (!(nextException instanceof SQLIntegrityConstraintViolationException)) {
throw nextException;
}
nextException = nextException.getNextException();
}
return false;
} catch (SQLException e) {
throw e;
} finally {
IOUtil.close(ps);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.seata.common.ConfigurationKeys;
import org.apache.seata.common.util.IOUtil;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.store.LockDO;
import org.apache.seata.server.storage.db.lock.LockStoreDataBaseDAO;
import org.apache.commons.dbcp2.BasicDataSource;
Expand Down Expand Up @@ -52,9 +54,9 @@ public static void start(ApplicationContext context){
dataSource.setUsername("sa");
dataSource.setPassword("");

ConfigurationFactory.getInstance().putConfig(ConfigurationKeys.STORE_DB_TYPE, "h2");
ConfigurationFactory.getInstance().putConfig(ConfigurationKeys.LOCK_DB_TABLE, "lock_table");
dataBaseLockStoreDAO = new LockStoreDataBaseDAO(dataSource);
dataBaseLockStoreDAO.setDbType("h2");
dataBaseLockStoreDAO.setLockTable("lock_table");

prepareTable(dataSource);
}
Expand Down Expand Up @@ -278,6 +280,57 @@ public void test_isLockable_cannot() throws SQLException {

}

@Test
public void test_isLockable_cannot1() throws SQLException {
List<LockDO> lockDOs = new ArrayList<>();
for(int i = 0; i < 3; i++) {
LockDO lock = new LockDO();
lock.setResourceId("abc");
lock.setXid("abc-123:222");
lock.setTransactionId(222L);
lock.setBranchId(1L);
lock.setRowKey("abc-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs.add(lock);
}

boolean ret = dataBaseLockStoreDAO.acquireLock(lockDOs, true, true);
Assertions.assertTrue(ret);

String sql = "select * from lock_table where xid = 'abc-123:222' and table_name = 't' and row_key in ('abc-0','abc-1','abc-2')" ;
Connection conn = null;
ResultSet rs = null;
try{
conn = dataSource.getConnection();
rs = conn.createStatement().executeQuery(sql);
if (rs.next()) {
Assertions.assertTrue(true);
} else {
Assertions.fail();
}
} finally {
IOUtil.close(rs, conn);
}

List<LockDO> lockDOs_2 = new ArrayList<>();
for(int i = 0; i < 3; i++) {
LockDO lock = new LockDO();
lock.setResourceId("abc");
lock.setXid("abc-123:333");
lock.setTransactionId(333L);
lock.setBranchId(2L);
lock.setRowKey("abc-"+i);
lock.setPk(String.valueOf(i));
lock.setTableName("t");
lockDOs_2.add(lock);
}

boolean ret2 = dataBaseLockStoreDAO.acquireLock(lockDOs_2, true, true);
Assertions.assertFalse(ret2);

}

@AfterAll
public static void clearStoreDB(){
FileUtils.deleteRecursive("db_store", true);
Expand Down
Loading