org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@10cf3d8 is closed。
2016-12-08 17:26
309 查看
描述:在使用hibernate操作数据库时报org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@10cf3d8 is closed。
启动程序后,首先执行了一个get,然后接着执行update是抛出上该异常。
原因:
操作Transaction时使用了ThreadLocal.在beginTransaction时,将新的transaction set到了ThreadLocal中。
由于在get时执行beginTransaction打开了一个事务,而未执行commitTransaction方法,导致事务未提交。
所以接下来的update时抛了异常。
解决方法:
get时不需要commit,所以不可以调用commitTransaction方法,单独写一个提交事务的方法,把ThreadLocal中的事务重置即可。
代码:
private static final ThreadLocal<Transaction> transactionThreadLocal = new ThreadLocal<Transaction>();
public static void beginTransaction() {
Transaction transaction = (Transaction) transactionThreadLocal.get();
try {
if (transaction == null) {
transaction = getSession().beginTransaction();
transactionThreadLocal.set(transaction);
}
} catch (HibernateException e) {
throw new HibernateException(e);
}
}
public static void commitTransaction() {
Transaction transaction = (Transaction) transactionThreadLocal.get();
try {
if (transaction != null) {
transaction.commit();
}
transactionThreadLocal.set(null);
} catch (HibernateException e) {
throw new HibernateException(e);
}
}
public static void commitNullTransaction() {
Transaction transaction = (Transaction) transactionThreadLocal.get();
try {
if (transaction != null) {
transactionThreadLocal.set(null);
}
} catch (HibernateException e) {
throw new HibernateException(e);
}
}
启动程序后,首先执行了一个get,然后接着执行update是抛出上该异常。
原因:
操作Transaction时使用了ThreadLocal.在beginTransaction时,将新的transaction set到了ThreadLocal中。
由于在get时执行beginTransaction打开了一个事务,而未执行commitTransaction方法,导致事务未提交。
所以接下来的update时抛了异常。
解决方法:
get时不需要commit,所以不可以调用commitTransaction方法,单独写一个提交事务的方法,把ThreadLocal中的事务重置即可。
代码:
private static final ThreadLocal<Transaction> transactionThreadLocal = new ThreadLocal<Transaction>();
public static void beginTransaction() {
Transaction transaction = (Transaction) transactionThreadLocal.get();
try {
if (transaction == null) {
transaction = getSession().beginTransaction();
transactionThreadLocal.set(transaction);
}
} catch (HibernateException e) {
throw new HibernateException(e);
}
}
public static void commitTransaction() {
Transaction transaction = (Transaction) transactionThreadLocal.get();
try {
if (transaction != null) {
transaction.commit();
}
transactionThreadLocal.set(null);
} catch (HibernateException e) {
throw new HibernateException(e);
}
}
public static void commitNullTransaction() {
Transaction transaction = (Transaction) transactionThreadLocal.get();
try {
if (transaction != null) {
transactionThreadLocal.set(null);
}
} catch (HibernateException e) {
throw new HibernateException(e);
}
}
相关文章推荐
- org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@17b9635 is closed
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.GenericJDBCException: Cannot open connection
- java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl
- hibernate4 org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure
- Caused by: java.lang.AbstractMethodError: org.hibernate.validator.internal.engine.ConfigurationImpl
- org.hibernate.exception.GenericJDBCException: Cannot open connection
- org.hibernate.exception.GenericJDBCException: Cannot release connection
- Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]
- code is 9998;desc is 插入失败exception is org.hibernate.exception.JDBCConnectionException: Could not op
- spring配置出错at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244
- 从数据库方面解决org.hibernate.exception.JDBCConnectionException:could not execute query
- code is 9998;desc is 插入失败exception is org.hibernate.exception.JDBCConnectionException: Could not op
- Oracle:org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.GenericJDBCException: Cannot release connection
- org.hibernate.exception.GenericJDBCException: Cannot open connection
- org.hibernate.exception.JDBCConnectionException: could not execute queryMySQL数据库连接超时
- org.hibernate.exception.GenericJDBCException: Cannot open connection
- WARN [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] - <HHH000342: Could not obtain connectio
- org.hibernate.exception.GenericJDBCException: Cannot open cconnection(执行几次查询之后就不能查询/页面动不了/分页查询只能点击几)