3
votes

If we go with programmatic transaction, we write

Session session=sessiongFactory.openSession();
Transaction tx=session.buildTransaction();

And for a session we can build as many transaction we want.

So, We have first session object than we get Transaction Object.

While in Declarative Transaction,If we declarative @Transaction annotation at service level. "When this Service Method will be called,Transaction will be Open" so here there is not any inforamtion about Session. Then in Dao we write

Session session=sessiongFactory.getCurrentSession();

Here we have first Transation then Session,

Can any one please help me in understanding ,How spring manages this Declarative Transaction.

4

4 Answers

4
votes

According to documentation method sessiongFactory.getCurrentSession() obtains the current session, and"current session" means controlled by the CurrentSessionContext impl configured for use.

Documentation also provides this explanation for backwards compatibility: if a CurrentSessionContext is not configured but a JTA TransactionManagerLookup is configured, this will default to the JTASessionContext impl.

JTASessionContext implementation will generate Sessions as needed provided a JTA transaction is in effect. If a session is not already associated with the current JTA transaction at the time currentSession() is called, a new session will be opened and it will be associated with that JTA transaction.

2
votes

With Spring declarative transaction management you can apply @Transactional at both method & class level. It is enabled via AOP proxies. The combination of AOP with transactional metadata yields an AOP proxy that uses a TransactionInterceptor in conjunction with an appropriate PlatformTransactionManager implementation to drive transactions around method invocations.

Conceptually, calling a method on a transactional proxy looks like this…

enter image description here

When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings.

All transactions are associated with the session. Transactions are initiated on the service layer but they have to be associated with a session to be committed. First transaction completes then session is closed. A session can also span several transactions. If you are using hibernate, spring uses hibernate managed transaction manager which is responsible for associating transactions with hibernate session.

1
votes

Spring transaction management abstract the transaction handling and decouples the transaction demarcation logic (e.g. @Transactional) from the actual transaction manager (e.g. RESOURCE_LOCAL, JTA).

The problem with programmatic transaction is that you tie your application code to the transaction management logic. On the other hand, Spring allows you to switch from JpaTransactionManager to JtaTransactionManager with just some configuration (no need to change the application code).

Spring only creates a transaction context that's used by the inner TransactionInterceptor to execute the actual transaction management hooks.

  1. For RESOURCE_LOCAL, transactions are handled through the JDBC Connection commit() and rollback() methods.

  2. For JTA, transactions are handled through the JTA UserTransaction commit() and rollback() methods.

0
votes

Everything is explained in the docs. You may also want to take a look at Spring integration with ORM.

Basically, Spring creates a proxy which intercepts the transactional methods invocation and starts the transaction before delegating the call to the target method, and ends the transaction when the target method returns.