开发者

hibernate transaction not rolling back correctly

开发者 https://www.devze.com 2023-03-24 14:31 出处:网络
I have 2 tables, say Item and Property and a hibernate object mapped to both. The mapping for table Item to Property looks like

I have 2 tables, say Item and Property and a hibernate object mapped to both. The mapping for table Item to Property looks like

<set name="propertySet" cascade="all-delete-orphan">
    <key column="item_id" not-null="true"/>
    <one-to-many class="Property"/>
</set>

An item can have multiple properties. Everything like select, insert works properly. but when there is an error, the inserts to the property table do not rollback.

What happens is that if i am editing an item with N properties and enter an invalid value in a field, the next time I retrieve the item, it has 2*N properties.

Edit ---

What my class looks like is

@Autowired
SessionFactory sessionFactory

@Transactional
public void updateItem(Item i){
    ...
    // The only 2 statements dealing with hibernate or session in this function
    ItemModel im = sessionFactory.getCurrentSession().get(...);

    sessionFactory.getCurrentSession().update(updatedItem);
    ...
}

I am using annotated transactions (@Transactional) with the spring framework and the lowest exception getting thrown is

Caused by: org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.Tra
nsactionException: Transaction not successfully started
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:679)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:412)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:111)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
Caused by: org.hibernate.TransactionException: Transaction not successfully started
        at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:676)
        at org.sprin开发者_StackOverflow中文版gframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:412)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:111)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
    ...
    ...
        at org.apache.catalina.valves.SSLValve.invoke(SSLValve.java:113)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11NioProcessor.process(Http11NioProcessor.java:894)
        at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:719)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:2101)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)


Transactions don't "cascade". At the JDBC level, a transaction consists of:

  1. Turning off autocommit
  2. Executing some statements
  3. Calling java.sql.Connection.commit() or java.sql.Connection.rollback().

If you're saying that some things are being committed and some are rolled back, then there's something wrong in your transaction management. Either autocommit is on or you actually have multiple calls to commit() happening.


Transactions are managed under the hood by Spring if you do the following: in your XML config, you need to 1) enable transactions, and 2) configure a transaction manager, as follows:

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mainSessionFactory" />
</bean>

Tx schemaLocation is http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"

0

精彩评论

暂无评论...
验证码 换一张
取 消