ssh 事务不能回滚 Could not roll back Hibernate transaction; nested exception is org.hibernate.Transaction

调试没问题,发布到服务器上一段时间报以下异常

org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: JDBC rollback failed
org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:677)

问题原因:mysql 数据库将hibernate 连接给关闭了,

解决1:

hibernate配置数据源

proxool数据源

在src目录下添加Proxool.xml文件与struts.xml hiberntate.xml同目录

<?xml version="1.0" encoding="UTF-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
  <proxool>
   <alias>DBPool</alias>
    <driver-url>jdbc:mysql://localhost:3306/house</driver-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <driver-properties>
      <property name="user" value="root"/>
      <property name="password" value="admin"/>
    </driver-properties>
    <maximum-connection-count>10</maximum-connection-count>
    <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> 
    </proxool>
</something-else-entirely>
hiberante配置 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
		<property name="hibernate.proxool.pool_alias">DBPool</property>
		<property name="hibernate.proxool.xml">Proxool.xml</property>
		<property name="show_sql">true</property>
		<!--不删除原数据-->
		<property name="hbm2ddl.auto">update</property>

		<mapping resource="com/Allean/bean2/UserBean.hbm.xml" />
		<mapping resource="com/Allean/bean2/HouseAPK.hbm.xml" />
	</session-factory>
</hibernate-configuration>

c3p0数据源

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		
		 <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
			<property name="hibernate.c3p0.max_size">20</property>
			<property name="hibernate.c3p0.min_size">5</property> 
			<property name="hibernate.c3p0.timeout">120</property> 
			<property name="hibernate.c3p0.max_statements">100</property> 
			<property name="hibernate.c3p0.idle_test_period">120</property> 
			<property name="hibernate.c3p0.acquire_increment">2</property> 
			<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
			<property name="hibernate.connection.password">admin</property> 
			<property name="hibernate.connection.url">jdbc:mysql://********2</property> 
			<property name="hibernate.connection.username">admin</property>
			<property name="hibernate.connection.password">admin</property>
			<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/house</property> 
			<property name="hibernate.connection.username">root</property>
			<property name="current_session_context_class">thread</property>
			<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 
			<property name="hibernate.hbm2ddl.auto">update</property> 


		<mapping resource="com/Allean/bean2/UserBean.hbm.xml" />
		<mapping resource="com/Allean/bean2/HouseAPK.hbm.xml" />
	</session-factory>
</hibernate-configuration>

二、修入mysql my.ini文件 找到这个位置

# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]
wait_timeout=1814400 
# The TCP/IP Port the MySQL Server will listen on
port=3306

主要是修秘诀wait-timeout  单位是秒 设的值表示21天

已标记关键词 清除标记
package cn.jbit.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import cn.jbit.entity.User; import cn.jbit.util.HibernateUtil; public class test { private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) { Session session=null; Transaction tx=null; try { Configuration cfg=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=cfg.buildSessionFactory(serviceRegistry); session=sessionFactory.openSession(); tx=session.beginTransaction(); User user=new User(); user.setName("liufang"); user.setBorn(sdf.parse("1990-08-06")); session.save(user); tx.commit(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (Exception e) { // TODO: handle exception if(session!=null){ session.getTransaction().rollback(); } }finally{ HibernateUtil.closeSession(session); } } } 报错信息: log4j:WARN No appenders could be found for logger (org.jboss.logging). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:200) at cn.jbit.test.test.main(test.java:43) 相应jar包如图所示: ![图片说明](https://img-ask.csdn.net/upload/201503/30/1427703883_43525.png) hibernate.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url ">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="show_sql">true</property> <!--<property name="hibernate.hbm2ddl.auto">update</property> --><mapping resource="cn/jbit/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration> User.hbm.xml配置文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Demonstrates use of the enhanced sequence-based identifier generator, with no performance optimizations (the DB is hit everytime when generating a value). --> <hibernate-mapping package="cn.jbit.entity"> <class name="User" table="t_user"> <id name="id" column="id"> <generator class="native"><!-- <param name="table_name">ID_SEQ_TBL_BSC_SEQ</param> --></generator> </id> <property name="name" type="string"/> <property name="born" type="timestamp"/> </class> </hibernate-mapping>
org.hibernate.TransactionException: commit failed at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:187) at com.dtdream.cloud.dao.common.AbstractHibernateDao.create(AbstractHibernateDao.java:161) at com.dtdream.cloud.dao.common.AbstractHibernateDao$$FastClassBySpringCGLIB$$c6f6a568.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) at com.dtdream.cloud.openstack.service.OpenstackServiceRetryAspect.TransctionRetryAbstractDaoAdvice(OpenstackServiceRetryAspect.java:31) at sun.reflect.GeneratedMethodAccessor174.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) at com.dtdream.cloud.dao.impl.AliyunOmsDao$$EnhancerBySpringCGLIB$$3b8f60a5.create(<generated>) at com.dtdream.cloud.service.impl.OpenstackOmsService.getEcsOmsData(OpenstackOmsService.java:298) at com.dtdream.cloud.service.impl.OpenstackOmsService.createOmsItem(OpenstackOmsService.java:68) at com.dtdream.cloud.controller.OmsController.job(OmsController.java:1196) at sun.reflect.GeneratedMethodAccessor798.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:116) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:180) ... 34 more Caused by: java.sql.SQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:140) at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106) at org.mariadb.jdbc.MySQLStatement.executeQueryEpilog(MySQLStatement.java:268) at org.mariadb.jdbc.MySQLStatement.execute(MySQLStatement.java:296) at org.mariadb.jdbc.MySQLStatement.execute(MySQLStatement.java:346) at org.mariadb.jdbc.MySQLConnection.commit(MySQLConnection.java:225) at com.mchange.v2.c3p0.impl.NewProxyConnection.commit(NewProxyConnection.java:1284) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112) ... 35 more Caused by: org.mariadb.jdbc.internal.common.QueryException: Deadlock found when trying to get lock; try restarting transaction at org.mariadb.jdbc.internal.mysql.MySQLProtocol.getResult(MySQLProtocol.java:982) at org.mariadb.jdbc.internal.mysql.MySQLProtocol.executeQuery(MySQLProtocol.java:1042) at org.mariadb.jdbc.MySQLStatement.execute(MySQLStatement.java:289) ... 39 more
当多用户同时提交操作,更新数据库时,会出现以下异常。不在并发时,可以提交事务成功。用的entitymanager.lock(entity, LockModeType.PESSIMISTIC_WRITE);engitymanager.lock(entity, LockModeType.PESSIMISTIC_WRITE);同时操作都会出现异常。怎么可以让事务强行提交或提交,不让回滚。谢谢解答 org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:522) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) ..... ..... at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744) Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73) at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:513) ... 59 more
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页