Transaction not successfully started错误的解决方法
public class TOpenSessionInViewFilter extends OpenSessionInViewFilter {
??? public Jacn_OpenSessionInViewFilter() {
??????? super.setFlushMode(FlushMode.AUTO);
??? }
??? protected void commitSession(Session session){
??? ??? session.getTransaction().commit();
??? }
??? protected void closeSession(Session session, SessionFactory sessionFactory) {
??????? session.flush();
??????? try {
??????? ??? commitSession(session);
??????? } catch (HibernateException e) {
??????????? // TODO Auto-generated catch block//
??????? ??? e.printStackTrace();
??????? }
???????
??????? super.closeSession(session, sessionFactory);
??? }
}
如果递归函数里,有个session.beginTransaction(),或者transaction.commit();的话……就会出现 “Transaction not successfully started”异常……解决办法也很简单,把除了session.beginTransaction();和 transaction.commit();以外的语句拿出来,做个方法,递归那个就好了……
描述:
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
分析:
这 个错误的产生是因为我在保存entity后提交事务用的session.getTransaction().commit()语 句,session.getTransaction()只是根据session获得一个Transaction实例,但是并没有启动它,所以它会告诉你 “Transaction启动失败”(Transaction not successfully started)
解决:
应该用 session.beginTransaction()代替 session.getTransaction(),session.beginTransaction()方法在获得一个Transaction后调用 其begin()方法,如果是请求创建一个新的“受控”Transaction,则启动这个Transaction,否则就直接使用已经存在的“受控 ”Transaction来完成工作。
?