mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
514bb54ea5
commit
9268dc220c
@ -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
|
||||
|
@ -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>
|
||||
* 在同一事务中,所有对数据库操作都是原子的,同时提交或者同时回滚
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user