Reputation: 2611
If a class is marked as @Transactional, does that mean that the boilerplate:
Transaction tx = session.getTransaction();
try {
...
...
...
tx.commit();
}
catch (Exception e) {
tx.rollBack();
}
can be omitted? My Hibernate-based code certainly works without it.
EDIT (FOLLOW UP): Then why do people keep writing the boilerplate within their methods, even when they have their classes annotated?
Upvotes: 1
Views: 132
Reputation: 242786
Yes, as long as
Transaction management is properly configured, see 13.3.3 Declarative transaction demarcation
Calls to that method don't violate limitations of AOP-based declarative transaction management, see 10.5.1 Understanding the Spring Framework's declarative transaction implementation (or you use advanced features such as AspectJ integration to overcome these limitations).
That is, all calls to that method are performed on an object obtained from Spring, and no calls are performed from another methods of the same class.
Session
is obtained inside a method by calling SessionFactory.getCurrentSession()
.
Upvotes: 3
Reputation: 160301
As long as you also have <tx:annotation-driven/>
, yes--that's what the annotation is for :)
The docs go in to great detail.
Upvotes: 1
Reputation: 28981
Of course, it's the main goal of the declarative transactions notion. Moreover, it's more flexible so that you can use different propagation levels to combine several methods in transactions.
Upvotes: 1