Announcement Announcement Module
Collapse
No announcement yet.
Need advice on @Transactional Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need advice on @Transactional

    I have annotated my a service method (meaning it is annotated with @RequestMapping) with @Transactional(readOnly=false), i.e. this method has two annotations: @RequestMapping and @Transactional.

    In the method, I explicitly throw a new RuntimeException() by catching unchecked exceptions bubbled up, just to be sure that a rollback can be triggered in case it is needed.

    The method functions correctly in my test cases, until the cases created for testing transaction management. One of such case is to fail an database insert statement, in which a database (jdbcTemplate) error did occur, caught by the method, and a RuntimeException was thrown.

    This piece of code works when I tested in a standalone environment (Eclipse, test run started by a "main" in the class). However, rollback did not occur in a server environment, where the method was executed by a web service call (recall that it is annotated by @RequestMapping). In fact, I could not detect any sign in my log (I use log4j) on server that transaction management is working in the server environment.

    My server runs jboss 6 (an Openshift environment).

    Because differences in standalone and server environments ("main block" v.s. web service call, and different dataSource defns), two different applicationContext.xml were used. I did make sure that two contexts are identical w.r.t transaction management, i.e. "txManager" definition, and "tx:annotation-driven" statement.

    Any advice on possible causes, and/or "what/where to check" are greatly appreciated.

    Best Regards,

    John

    code segment (modified for clarity)
    Code:
    	@RequestMapping(value="billerConfigUpdate", method=RequestMethod.POST, 
    	                produces="application/json", consumes="application/json")	
    	@Transactional(readOnly=false, propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
    	public @ResponseBody ActivityStatus billerConfigUpdate(@RequestBody String req_body) {
    		ActivityStatus ac = new ActivityStatus();
    		
    		try {		
    		         configUpdate(....);   /* database ops are in this plain, i.e. unannotated, java method */
    		} catch (RuntimeException rte) {  
    			logger.error("Error in billerConfigUpdate -- transaction rollback " + rte.getMessage());
    			throw new RuntimeException();
    		}		
    		return ac;
    	}

  • #2
    Since i have received no response, I am rephrasing my "plea-for-help" to see if I can make my question clearer.

    To manage transactions by annotation, I performed 3 things:
    1. I added the following line to my applicationContext.xml:
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
    2. I annotated the methods which are transactional, mostly by
    @Transactional (readOnly=false, propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
    3. In my application, whenever an event should result in a rollback, I explicitly throw a runtime exception
    "throw new RuntimeException();"

    When I tested on my local server on which the (@Transactional) annotated method was called from main, rollback seems fine.
    However, when I tested it on a server, on which the the annotated method was called as a service (the method is also annotated by "@RequestMapping", rollback did not happen. More intriguingly, the RuntimeException was not intercepted by proxy (because it showed up in log) and I could find any sign in the log that transaction management was ever activated.

    Question: because above mentioned 3 things, do I need to do anything else in order to activate transaction management (by annotation) in a server environment? Have I missed any subtle things?

    Your insights are greatly appreciated.

    John

    Comment


    • #3
      Hi John

      I'm also struggling with a similar problem, that the expected behavior of a rollback is ignored:

      http://forum.springsource.org/showth...light=rollback

      Could you post the config of your Transaction Handling.

      baeschti

      Comment

      Working...
      X