`

Could not commit Hibernate transaction,Transaction not successfully started

阅读更多

SessionFactory.getCurrentSession() update 数据出错:

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started

at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)

at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

at $Proxy45.deliveryBatch(Unknown Source)

at com.yuxutech.thirdpart.action.SellerBizInfoAction.deliveryBatch(SellerBizInfoAction.java:61)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)

at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)

at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)

at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)

at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)

at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)

at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)

at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)

at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)

at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)

at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)

at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)

at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)

at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

at java.lang.Thread.run(Thread.java:619)

Caused by: org.hibernate.TransactionException: Transaction not successfully started

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:131)

at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)

... 79 more

 

解决方案一,来源:http://www.blogjava.net/super-nini/archive/2009/09/11/294762.html

先看看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();   
   }

问题解决

 

按照方案一改后,还是报错,为:

   org.hibernate.SessionException: Session is closed!

看来我的错误不是在这儿了。

 

然后查到 http://blog.csdn.net/woisnong/article/details/7423962

Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

改为

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

 

这样在try{}acatch{}后加上finally{session.close();}问题解决

 

 

分享到:
评论

相关推荐

    Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ….. this is incompatible with sq

    1、写在开头 标题之前我想说一下Linux的mysql真的实在是太坑了。太坑了。总是会出现这样那样的你想不到的问题。崩溃了。首先来罗列一下我遇到过的一些问题吧。 1、大小写敏感 2、连接数超过系统最大连接数 ...

    微软内部资料-SQL性能优化3

    In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...

    SQL Server 事务 游标 基本语法操作实例 全SQL语句

    PRINT 'Transaction committed successfully.'; END ``` **二、SQL Server游标** 游标允许在结果集中逐行处理数据,这在需要遍历结果集或进行迭代操作时非常有用。在SQL Server中,游标有以下几种类型: 1. **...

    基于GrégoireCachet的优秀要点的Django事务的提交和回滚信号。_Python_下载.zip

    print("Transaction committed successfully.") @receiver(post_rollback) def on_transaction_rollback(sender, **kwargs): print("Transaction rolled back.") ``` 7. 何时使用事务信号 - 大型数据操作:...

    C#关于事务的使用和源代码

    Console.WriteLine("Transaction committed successfully."); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); transaction.Rollback(); Console.WriteLine("Transaction ...

    nodejs-mysql-transaction:NodeJS MySQL事务的演示仓库

    console.log('Transaction completed successfully!'); }); }); }); }); connection.end(); ``` 8. **连接池(Pool)** 在实际应用中,为了提高性能和资源利用率,通常会使用连接池来管理数据库连接。`...

    sqlserver 函数、存储过程、游标与事务模板

    PRINT 'Transaction committed successfully.' END ``` 在实际应用中,这些组件通常结合使用,以实现更复杂的业务逻辑和数据处理。了解并熟练掌握这些概念对于SQL Server的使用者来说至关重要。通过创建和使用...

    Java事务的简单代码

    System.out.println("Transaction committed successfully."); } } catch (SQLException e) { e.printStackTrace(); } } private static boolean someConditionFails() { // 这里可以是任何可能导致失败的...

    PHP及数据库PPT课件

    echo "Transaction completed successfully."; } else { throw new Exception("Error in SQL: " . mysqli_error($conn)); mysqli_rollback($conn); } } catch (Exception $e) { echo "Transaction rolled back...

    数据库测试快速新增数据

    $pdo-&gt;beginTransaction(); foreach ($data as $row) { $stmt-&gt;execute($row); } if ($stmt-&gt;errorCode() === 0) { $pdo-&gt;commit(); echo "All records inserted successfully"; } else { $pdo-&gt;rollback(); ...

    node-mysql学习.zip

    console.log('Transaction completed successfully!'); }); }); ``` 此外,`node-mysql`库还有其他高级特性,如连接池管理,错误处理,以及自定义函数和预编译语句等。理解这些概念和API将使你在Node.js项目中与...

    存储过程实例解释 事务实例解释 C#中使用带返回值的存储过程

    transaction.Commit(); return "Password updated successfully."; } catch (Exception ex) { // 如果发生异常,则回滚事务 transaction.Rollback(); return "Failed to update password: " + ex.Message; ...

    C# 连接 mysql 实现显示,查找,删除操作

    cmd.Transaction.Commit(); // 提交事务 } catch (Exception ex) { cmd.Transaction.Rollback(); // 回滚事务,如果发生错误 Console.WriteLine("Error: " + ex.Message); } finally { connection.Close(); } ``...

    ios读写数据库例子

    通过`BEGIN`, `COMMIT` 和 `ROLLBACK` 命令管理事务。 7. **关闭数据库** - 在完成所有操作后,别忘了调用`sqlite3_close()`关闭数据库连接。 在iOS的SQLite操作中,为了简化上述过程,通常会使用第三方库,如...

    JavaScript_一个用于PostgreSQL、MySQL、CockroachDB、SQL Server、SQLite3和

    .then(() =&gt; console.log('Transaction completed successfully')) .catch((err) =&gt; console.error('Transaction failed:', err)); ``` 在这个例子中,如果任何步骤出错,整个事务将被回滚,确保数据一致性。 除了...

    Nodejs_MSSQL

    console.log('Transaction completed successfully.'); sql.close(); }); } }); } }); }); }).catch((err) =&gt; console.log(err)); ``` 在处理大型结果集时,可以使用`request.stream()`方法来流式读取数据...

    java上传excel并且解析入库

    return ResponseEntity.ok("File uploaded successfully."); } ``` 接着,我们需要解析上传的Excel文件。Apache POI是一个强大的Java库,用于读写Microsoft Office格式的文件,包括Excel。使用POI,我们可以轻松地...

    node mysql

    console.log('Transaction completed successfully!'); }); }); }); }); ``` 8. **错误处理** 任何数据库操作都可能抛出错误,因此推荐在每个查询后面都使用`try/catch`块,或者在回调函数中处理错误。当...

    PHP mysqli 操作数据库.rar

    使用`mysqli_begin_transaction()`, `mysqli_commit()`, 和 `mysqli_rollback()`进行事务管理。例如: ```php mysqli_begin_transaction($conn); try { // 执行操作 mysqli_query($conn, $sql1); mysqli_query...

Global site tag (gtag.js) - Google Analytics