0
votes

When I execute the code, its showing org.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only. But its not rolling back.

My Code:

Service level

public void checkTransaction(Users user) throws Exception {
    adminDao.insertUser(user);
    System.out.println("Transaction active :: " + TransactionSynchronizationManager.isActualTransactionActive());
    throw (new Exception("Testing Transaction"));
}

xml

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
    </tx:attributes>
</tx:advice>

<tx:annotation-driven proxy-target-class="true"
    transaction-manager="transactionManager" />

<aop:config>
    <aop:pointcut id="serviceOperation"
        expression="execution(* myapp.admin.service.AdminService.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>

POJO

My POJO (Users) Bean is not a annotated one, its mapped to hbm file. But it is registered in mapping resources.

<bean id="mysessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="mappingResources">
        <list>
            <value>myapp/admin/vo/Users.hbm.xml</value></list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
1
rollback-for="java.lang.Exception" maybe? - msp
Nope. Not working. Showing the same error. - Jegatheesh M
There could be an exception in your adminDao.insertUser(user) method. Check if its working. - Abdullah Khan
Actually its a test function. I want to know whether it is working properly or not. In my case 'adminDao.insertUser(user)' is working fine, its inserting data into database. Next line is throwing excetion(manually- for checking purpose). According to my transaction advice its need to roll back, but its not. - Jegatheesh M
Anyone please? Help me to solve - Jegatheesh M

1 Answers

0
votes

I was suffering from the same problem. What happens is that in the Service layer you can not make a query of type select and after it, an insert. You need to separate the processes. In my case, declare a component class and, through it, call the processes of my service separately. I hope it is useful for you ;)