Both TransactionTemplate and TransactionInterceptor delegate the actual transaction handling to a
PlatformTransactionManager instance, which can be a HibernateTransactionManager (for a single
Hibernate SessionFactory, using a ThreadLocal Session under the hood) or a JtaTransactionManager
(delegating to the JTA subsystem of the container) for Hibernate applications. You could even use a custom
PlatformTransactionManager implementation. So switching from native Hibernate transaction management to
JTA, such as when facing distributed transaction requirements for certain deployments of your application, is
just a matter of configuration. Simply replace the Hibernate transaction manager with Spring's JTA transaction
implementation. Both transaction demarcation and data access code will work without changes, as they just use
the generic transaction management APIs.
For distributed transactions across multiple Hibernate session factories, simply combine
JtaTransactionManager as a transaction strategy with multiple LocalSessionFactoryBean definitions. Each
of your DAOs then gets one specific SessionFactory reference passed into it's respective bean property. If all
underlying JDBC data sources are transactional container ones, a business service can demarcate transactions
across any number of DAOs and any number of session factories without special regard, as long as it is using
JtaTransactionManager as the strategy
Partager