转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题

本文探讨了在使用Hibernate操作CLOB时出现的TransactionSystemException异常,并提供了详细的解决方案,包括使用openSession替代getCurrentSession的方法。

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
最近在写操作CLOB的过程中总是报这个异常,数据去插入进去了.


先看看SessionFactory.getCurrentSession与openSession的区别
1. 如果使用的是getCurrentSession来创建session的话,在commit后,session就自动被关闭了,
    也就是不用再session.close()了。但是如果使用的是openSession方法创建的session的话,
    那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭
/*2. getCurrentSession的使用可以参见hibernate\hibernate-3.2\doc\tutorial\src项目
3. 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:
   * 如果采用jdbc独立引用程序配置如下:
    <property name="hibernate.current_session_context_class">thread</property>
   * 如果采用了JTA事务配置如下
    <property name="hibernate.current_session_context_class">jta</property>*/

开始用的:

Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();

好多文章里都说只需在Spring配置<bean id="sessionFactory"...---><property name="hibernateProperties">   中加入 <prop key="hibernate.current_session_context_class">thread</prop>就可以解决,试了没用.

修改后的:

Session session = this.getHibernateTemplate().getSessionFactory().openSession();

Transaction tran=session.beginTransaction();

......

finally
   {  
    session.close();  
   }

问题解决

### 解决 'Could not open Hibernate Session for transaction' GenericJDBCException 错误原因及方案 #### 一、错误分析 当应用程序尝试启动一个新的数据库事务时,如果无法创建或获取到有效的数据库连接,则会抛出 `GenericJDBCException` 异常。具体表现为 "could not open hibernate session for transaction"[^1] 或者 HTTP状态码500伴随相同的信息[^2]。 #### 二、常见原因 1. **数据源配置不正确** 数据库驱动类名拼写有误、URL路径不对或是用户名密码设置不当都可能导致应用服务器找不到合适的数据源来建立连接。 2. **网络问题** 如果数据库位于远程主机上,任何影响客户端与服务端之间通信的因素都会造成此异常。这可能涉及到防火墙阻止特定端口流量、DNS解析失败等问题。 3. **资源耗尽** 当存在过多未关闭的Connection对象时,可能会触发池满的情况,从而使得新的请求得不到响应并最终超时引发异常。 4. **权限不足** 用户账户缺乏执行某些操作所需的适当授权也可能引起类似的错误消息。 #### 三、排查方法 为了定位确切的原因,建议按照如下顺序逐步检查: - 查看日志文件中的堆栈跟踪信息以获得更详细的上下文; - 验证application.properties/application.yml中关于DataSource的相关属性是否准确无误; - 使用命令行工具测试能否成功连通目标数据库实例; - 审查当系统的最大并发数限制以及正在运行的任务数量; - 确认所使用的账号具备足够的权利来进行预期的操作; #### 四、解决方案 针对上述每种可能性采取相应的措施可以有效解决问题: ```properties # application.properties 示例 spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 对于Spring Boot项目来说,确保以上参数被正确定义于配置文件内是非常重要的。另外,在编写业务逻辑代码时也要注意合理管理Session生命周期,比如通过try-with-resources语句自动释放资源: ```java // Java代码片段展示如何安全地处理session @Autowired private EntityManagerFactory entityManagerFactory; public void performDatabaseOperation() { try (EntityManager em = entityManagerFactory.createEntityManager()) { EntityTransaction tx = em.getTransaction(); tx.begin(); // 执行CRUD操作... tx.commit(); } catch (RuntimeException e) { throw new RuntimeException(e); } } ``` 最后但同样重要的是定期维护数据库性能指标监控体系,以便及时发现潜在瓶颈所在,并据此调整优化策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值