This commit is contained in:
Looly 2022-05-01 01:32:38 +08:00
parent 514bb54ea5
commit 9268dc220c
4 changed files with 44 additions and 50 deletions

View File

@ -35,10 +35,9 @@ import java.util.Map;
*
* @author looly
*/
public abstract class AbstractDb<R extends AbstractDb<R>> implements ConnectionHolder, Serializable {
public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnectionHolder implements Serializable {
private static final long serialVersionUID = 3858951941916349062L;
protected final DataSource ds;
/**
* 是否支持事务
*/
@ -58,7 +57,7 @@ public abstract class AbstractDb<R extends AbstractDb<R>> implements ConnectionH
* @param dialect 数据库方言
*/
public AbstractDb(final DataSource ds, final Dialect dialect) {
this.ds = ds;
super(ds);
this.runner = new DialectRunner(dialect);
}
// ------------------------------------------------------- Constructor end

View File

@ -106,29 +106,6 @@ public class Db extends AbstractDb<Db> {
}
// ---------------------------------------------------------------------------- Constructor end
@Override
public Connection getConnection() throws DbRuntimeException {
try {
return ThreadLocalConnection.INSTANCE.get(this.ds);
} catch (final SQLException e) {
throw new DbRuntimeException(e);
}
}
@Override
public void closeConnection(final Connection conn) {
try {
if (conn != null && false == conn.getAutoCommit()) {
// 事务中的Session忽略关闭事件
return;
}
} catch (final SQLException e) {
// ignore
}
ThreadLocalConnection.INSTANCE.close(this.ds);
}
/**
* 执行事务使用默认的事务级别<br>
* 在同一事务中所有对数据库操作都是原子的同时提交或者同时回滚

View File

@ -0,0 +1,42 @@
package cn.hutool.db;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* 默认的连接持有器
*
* @author looly
*/
public class DefaultConnectionHolder implements ConnectionHolder {
protected final DataSource ds;
public DefaultConnectionHolder(DataSource ds) {
this.ds = ds;
}
@Override
public Connection getConnection() throws DbRuntimeException {
try {
return ThreadLocalConnection.INSTANCE.get(this.ds);
} catch (final SQLException e) {
throw new DbRuntimeException(e);
}
}
@Override
public void closeConnection(final Connection conn) {
try {
if (conn != null && false == conn.getAutoCommit()) {
// 事务中的Session忽略关闭事件
return;
}
} catch (final SQLException e) {
// ignore
}
ThreadLocalConnection.INSTANCE.close(this.ds);
}
}

View File

@ -268,30 +268,6 @@ public class Session extends AbstractDb<Session> implements Closeable {
// ---------------------------------------------------------------------------- Transaction method end
@Override
public Connection getConnection() throws DbRuntimeException {
try {
return ThreadLocalConnection.INSTANCE.get(this.ds);
} catch (final SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void closeConnection(final Connection conn) {
try {
if(conn != null && false == conn.getAutoCommit()) {
// 事务中的Session忽略关闭事件
return;
}
} catch (final SQLException e) {
log.error(e);
}
// 普通请求关闭或归还连接
ThreadLocalConnection.INSTANCE.close(this.ds);
}
@Override
public void close() {
closeConnection(null);