Working Spring TransactionInterceptor with @Transactional annotation support

778 views
Skip to first unread message

IvanoBulo

unread,
Aug 14, 2007, 7:21:34 AM8/14/07
to google-guice
Hi,

Have spent couple of hours to make Spring TransactionInterceptor work
properly with @Transactional annotation in the way it is described in
Spring documentation. H.e. to support both class and method
annotations.

The main problem I've met was with
AnnotationTransactionAttributeSource class which always provides class-
level transaction attributes but not the one I've specified for my
method. It worked only if there were no class @Transactional
annotation. And the second problem was minor: support class level
annotation for method without interceptor which was resolved by single
statement

binder.bindInterceptor(annotatedWith(Transactional.class),
not(annotatedWith(Transactional.class)), transactionInterceptor);

The solution is following:

...
final TransactionInterceptor transactionInterceptor = new
TransactionInterceptor(
transactionManager, new MyAnnotationTransactionAttributeSource()
);
binder.bindInterceptor(any(), annotatedWith(Transactional.class),
transactionInterceptor);
binder.bindInterceptor(annotatedWith(Transactional.class),
not(annotatedWith(Transactional.class)), transactionInterceptor);
...

public class MyAnnotationTransactionAttributeSource extends
AnnotationTransactionAttributeSource {
@Override
public TransactionAttribute getTransactionAttribute(Method method,
Class aClass) {
return super.getTransactionAttribute(method,
method.getDeclaringClass());
}
}

So my following service works as expected: read-only transactions for
all methods except save(User user)

@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
private final UserDAO _userDAO;

@Inject
public UserServiceImpl(UserDAO userDAO) {
_userDAO = userDAO;
}

public List<User> getUsers() {
return _userDAO.findAll();
}

public User getUser(Long id) {
return _userDAO.findById(id);
}

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly =
false)
public void save(User user) {
_userDAO.saveOrUpdate(user);
}
}

P.S. I'm very happy about Guice :)

Dhanji R. Prasanna

unread,
Aug 14, 2007, 7:33:59 AM8/14/07
to google...@googlegroups.com
If you're using Hibernate or JPA consider Warp-persist which has similar transactional semantics:

http://www.wideplay.com/guicewebextensions2

        @Transactional(propagation = Propagation.REQUIRES_NEW , readOnly =

IvanoBulo

unread,
Aug 14, 2007, 8:35:01 AM8/14/07
to google-guice
I saw it. To me it is overhead to use your library plus I didn't see
any benefits and I can't easily integrate it with my maven build.

- Ivan

On Aug 14, 2:33 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> If you're using Hibernate or JPA consider Warp-persist which has similar
> transactional semantics:
>
> http://www.wideplay.com/guicewebextensions2
>

> > @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly =

Dhanji R. Prasanna

unread,
Aug 14, 2007, 9:32:45 AM8/14/07
to google...@googlegroups.com
Ahh but spring does not have dynamic finders. ;)
I imagine a 37k lib is not too hard to integrate...even with maven. whatever works for you of course.

Dhanji.

On 8/14/07, IvanoBulo <ivan...@gmail.com> wrote:

dani

unread,
Aug 15, 2007, 9:47:32 AM8/15/07
to google-guice

in defense of warp-persist: I'm currenlty using wapr-persist in a
couple of projects, all with maven and i have to say that i'm very
happy: it does exaclty what i need without to worry about thouse
minimal details that stops always my development.


Reply all
Reply to author
Forward
0 new messages