TRANSACTION IN HIBERNATE

Posted on by By Nikhilesh, in Miscellaneous | 0

Transaction: the term transaction has many different yet related meanings in regards to persistence and Object/Relational Mapping. In most use-cases these

definitions align, but that is not always the case.

  • Might refer to the physical transaction with the database.
  • Might refer to the logical notion of a transaction as related to a persistence context.
  • Might refer to the application notion of a Unit-of-Work, as defined by the archetypal pattern.

 

 

Hibernate uses the JDBC API for persistence. In the world of Java there are 2 well defined mechanism for dealing with transactions in JDBC: JDBC itself and JTA.

Hibernate supports both mechanisms for integrating with transactions and allowing applications to manage physical transactions.

image

The first concept in understanding Hibernate transaction support is the org.hibernate.engine.transaction.spi. TransactionFactory interface which serves 2 main functions:

  • It allows Hibernate to understand the transaction semantics of the environment. Are we operating in a JTA environment? Is a physical transaction already currently active? etc.
  • It acts as a factory for org.hibernate.Transaction instances which are used to allow applications to manage and check the state of transactions. org.hibernate.Transaction is Hibernate’s notion of a logical transaction. JPA has a similar notion in the javax.persistence.EntityTransaction interface.

 

 

Note

javax.persistence.EntityTransactionis only available when using resource-local transactions.

Hibernate allows access to org.hibernate.Transaction regardless of environment.

org.hibernate.engine.transaction.spi.TransactionFactoryis a standard Hibernate service.

 

Physical Transactions – JDBC:

JDBC-based transaction management leverages the JDBC defined methods java.sql.Connection.commit()and java.sql.Connection.rollback()(JDBC does not define an

explicit method of beginning a transaction). In Hibernate, this approach is represented by the org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory

 

Physical Transactions – JTA

JTA-based transaction approach which leverages the javax.transaction.UserTransaction interface as obtained from

org.hibernate.service.jta.platform.spi.JtaPlatformAPI. This approach is represented by the

org.hibernate.engine.transaction.internal.jta.JtaTransactionFactoryclass.

 

Physical Transactions – CMT

Another JTA-based transaction approach which leverages the JTA javax.transaction.TransactionManagerinterface as obtained from

org.hibernate.service.jta.platform.spi.JtaPlatformAPI. This approach is represented by the

org.hibernate.engine.transaction.internal.jta.CMTTransactionFactoryclass. In an actual JEE CMT environment, access to the

javax.transaction.UserTransactionis restricted.

 

Physical Transactions – Custom

Its is also possible to plug in a custom transaction approach by implementing the org.hibernate.engine.transaction.spi.TransactionFactorycontract. The default service

initiator has built-in support for understanding custom transaction approaches via the hibernate.transaction.factory_classwhich can name either:

  • The instance of org.hibernate.engine.transaction.spi.TransactionFactory to use.

 

  • The name of a class implementing   org.hibernate.engine.transaction.spi.TransactionFactory to use.

The expectation is that the implementation class have a noargument

 

 

Physical Transactions – Legacy

During development of 4.0, most of these classes named here were moved to new packages. To help facilitate upgrading, Hibernate will also recognize the legacy names here for a short period of time.

  • org.hibernate.transaction.JDBCTransactionFactoryis mapped to org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
  • org.hibernate.transaction.JTATransactionFactoryis mapped to org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory
  • org.hibernate.transaction.CMTTransactionFactoryis mapped to org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments