Could not commit Hibernate 异常问题,spring 事物 提交前session关闭问题
2016-03-29 14:45
609 查看
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();
}
问题解决
最近在写操作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();
}
问题解决
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- Ruby中的异常处理代码编写示例
- MySQL抛出Incorrect string value异常分析
- 浅谈C#中简单的异常引发与处理操作
- 详解C#编程中异常的创建和引发以及异常处理
- 详解JavaScript中的异常处理方法
- Hibernate Oracle sequence的使用技巧
- 模拟Spring的简单实现
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- java程序中的延时加载异常及解决方案
- 解析Java异常的栈轨迹及其相关方法
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总