Using and Managing Spring Transactions in Struts 2 Actions

Hi guys, I’ve been working on a project for some time with the following components:

  • Struts2.1.8.1,
  • Spring 3.0.3
  • JPA 2.0,
  • Hibernate 3

I use Spring EntityManager magic ... But I am having transaction problems inside my actions. For example, I set values ​​on my saved object in several ways in my class, and I want to be able rollbackto if the method validatedetects a validation error, or committhese changes otherwise. I have read half the Internet for a long time to get a comprehensive explanation. Unfortunately, no complete examples exist (at least it looks like my stack).

I stumbled upon this thread on the mailing list: @Transactional Spring Annotation in action Struts2 does not work . The message I am linking seems to have a fairly simple and straightforward solution, using it TransactionInterceptorwill do the trick that seems ... The problem is that I cannot find useful information about this interceptor.

Does anyone have any experience with this technology and can you consult a link or two on how to use Spring transactions inside Struts2 actions?

Thank!

- Change 1 -

I created a test project, if you're interested, just upload the file and try it (or check). Thank!

+5
source share
3

, // beans/etc . - - - . ( ) . . . , bean, - userService.register(user). , spring :

<tx:annotation-driven /> @Transactional (btw, , <tx:..>, , . , )

+4

, ... , ( ... ).

, , Struts 2 Spring -JPA-Hibernate, @Transactional . , ( , ) , . , , .

, , , . , , , , .

, , .

+1

, .

. , . , .

, :

  • try/catch
  • , - , - ( action.hasErrors)

You probably want this hook to be defined fairly early on the stack. I do not know about any pre-built interceptors for this (although there may be some), but it is quite easy to assemble.

Update

I do not use Spring, so I can’t say how JPA transaction support works, but you can process transactions for your EntityManager, for example:

try {
    entityManager.getTransaction().begin();
    // do your thing
    entityManager.getTransaction().commit();
} catch (Exception e) {
    entityManager.getTransaction().rollback();
    throw new PersistenceException(e);
}

This is just a crude example, but it should illustrate the point.

0
source

All Articles