Announcement Announcement Module
Collapse
No announcement yet.
Spring @transactional not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring @transactional not working

    We have a little problem configuring transactions in a new springMVC+Hibernate application

    We are using spring 3.2 + Hibernate 4.0.

    The application is supposed to connect to two databases so we have the following configuration files
    1- primary_hibernateContext.xml -> for configuring the primary data source, primary session factory, and primary transaction manager
    2- secondary_hibernateContext.xml -> for configuring the secondary data source, secondary session factory, and secondary transaction manager.
    3- root-context.xml -> for configuring component scan
    4- mr2m-servlet.xml -> the only dispatcher servlet.

    In the root context xml file we have configured component scan as following
    Code:
    <tx:annotation-driven/>
    	<!-- Enable Spring annotation configuration -->
    	<context:annotation-config />
    	<!-- Scan the application for all possible Services & autowire -->
    	<context:component-scan base-package="net.mr2.*.service, net.mr2.*.dao" />
    In the mr2m servlet xml file we have configured another component scan for scanning the controllers as following
    Code:
    <context:component-scan base-package="net.mr2.*.web" use-default-filters="false">
    	    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    We have created a simple controller, service , and two DAOs (one for saving in the primary DB, and the other for saving in the secondary DB).

    The service class is annotated
    Code:
    @Transactional(value="secondary_transactionManager", readOnly=false , rollbackFor=Exception.class)
    and
    Code:
    @Service
    so we assume that the transactions is configured but we made a little test to check if the transaction is working or not.

    In the controller we call three methods in the service class
    Code:
    (save1(), save2(), save3())
    . Each method inserts a new record in the DB. In the third method we throw an exception, so we assume that (if the transaction is configured properly) the first two methods will execute while the third one will rollback.

    But all the three methods are executed and 3 records inserted into the DB. Also we have checked the mysql logs and found that the three insert statements are not between
    Code:
    SET AUTOCOMMIT
    statements.

    So, we are sure now that the transactions are not working as it is supposed to be.

    What would be the problem here ?

    Thanks in advance.
Working...
X