Skip to content

Commit

Permalink
解决开启事务失败时, 引用没有回收的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy0v0 committed Nov 7, 2024
1 parent b67d37a commit e6fa127
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 63 deletions.
9 changes: 5 additions & 4 deletions jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public final class JDBC {
static final Map<Class<?>, TypeSetHandler<?>> SetHandlerMap = new ConcurrentHashMap<>();

static {
ConnectionPoolUtil.pool = ServiceLoader.load(ConnectionPool.class)
.findFirst()
.orElse(new DefaultConnectionPool());
LogUtil.common().debug("使用的ConnectionPool: {}", ConnectionPoolUtil.pool.getClass().getName());

configuration(new ByteTypeHandler());
configuration(new ShortTypeHandler());
configuration(new IntegerTypeHandler());
Expand All @@ -47,10 +52,6 @@ public final class JDBC {

public static void configuration(DataSource dataSource) {
JDBC.dataSource = dataSource;
ConnectionPoolUtil.pool = ServiceLoader.load(ConnectionPool.class)
.findFirst()
.orElse(new DefaultConnectionPool());
LogUtil.common().debug("使用的ConnectionPool: {}", ConnectionPoolUtil.pool.getClass().getName());
}

public static DataSource getDataSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ T execute() throws SQLException {
LogUtil.performance().info("transaction begin");
long startTime = System.nanoTime() / 1000;
ConnectionReference ref = ConnectionPoolUtil.getReference(dataSource);
ConnectionTransaction transaction = ref.getTransaction(level);
try {
ConnectionTransaction transaction = ref.getTransaction(level);
try {
T result;
if (null != supplier) {
Expand Down
67 changes: 67 additions & 0 deletions jdbc/src/test/java/test/jdbc/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package test.jdbc;

import com.github.fantasy0v0.swift.jdbc.JDBC;
import com.github.fantasy0v0.swift.jdbc.exception.SwiftSQLException;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static com.github.fantasy0v0.swift.jdbc.JDBC.*;

public class TransactionTest {

private final Logger log = LoggerFactory.getLogger(TransactionTest.class);

@TestFactory
List<DynamicTest> testAllDatabase() {
return ContainerUtil.testAllContainers(() -> List.of(
new JdbcTest("test", this::test),
new JdbcTest("rollback", this::rollback)
));
}

void test(DataSource dataSource) throws SQLException {
String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName();

JDBC.configuration(dataSource);
try {
transaction(() -> {
select("select * from student").fetch();
transaction(Connection.TRANSACTION_READ_UNCOMMITTED, () -> {
select("select * from student").fetch();
transaction(Connection.TRANSACTION_READ_COMMITTED, () -> {
modify("update student set name = ? where id = ?")
.execute("修改", 1L);
});
});
});
} catch (Exception e) {
if (driverClassName.contains("postgresql")) {
Assertions.assertEquals(SwiftSQLException.class, e.getClass());
} else {
Assertions.fail("错误的分支");
}
// MySQL虽然不会报错, 但是修改属于无效操作
// 具体查看https://github.com/fantasy0v0/swift/issues/8#issuecomment-2461235425
}
}

void rollback(DataSource dataSource) {
JDBC.configuration(dataSource);
transaction(() -> {
modify("update student set name = ? where id = ?")
.execute("修改", 1L);
});
}

}
58 changes: 0 additions & 58 deletions jdbc/src/test/java/test/mysql/TransactionTest.java

This file was deleted.

0 comments on commit e6fa127

Please sign in to comment.