mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix code
This commit is contained in:
parent
514bb54ea5
commit
9268dc220c
@ -35,10 +35,9 @@ import java.util.Map;
|
|||||||
*
|
*
|
||||||
* @author looly
|
* @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;
|
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 数据库方言
|
* @param dialect 数据库方言
|
||||||
*/
|
*/
|
||||||
public AbstractDb(final DataSource ds, final Dialect dialect) {
|
public AbstractDb(final DataSource ds, final Dialect dialect) {
|
||||||
this.ds = ds;
|
super(ds);
|
||||||
this.runner = new DialectRunner(dialect);
|
this.runner = new DialectRunner(dialect);
|
||||||
}
|
}
|
||||||
// ------------------------------------------------------- Constructor end
|
// ------------------------------------------------------- Constructor end
|
||||||
|
@ -106,29 +106,6 @@ public class Db extends AbstractDb<Db> {
|
|||||||
}
|
}
|
||||||
// ---------------------------------------------------------------------------- Constructor end
|
// ---------------------------------------------------------------------------- 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>
|
* 执行事务,使用默认的事务级别<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
|
// ---------------------------------------------------------------------------- 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
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
closeConnection(null);
|
closeConnection(null);
|
||||||
|
Loading…
Reference in New Issue
Block a user