Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to understand transaction.factory_class of Hibernate configuration

In my hibernate.cfg.xml file, one of the properties is -

<property name = "transaction.factory_class"> <!--1-->
org.hibernate.transaction.JDBCTransactionFactory <!--2-->
</property> <!--3-->

Other properties are easy to understand. But, many questions came to my mind when I saw the above property.

line 1 - this specifies the class implementing the Transaction*Factory* interface.

Q1 - I saw the java docs for TransactionFactory but did not understand what it really is. What does this "Factory" mean ? Why don't they call it TransactionGenerator as per the line -

Contract for generating Hibernate Transaction instances.

Q2 - TransactionFactory lead me to Transaction. Is this exactly the same as a Database transaction ?

Q3 -

A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the notion of a transaction. However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time.

...the notion of a session is of coarser granularity than the notion of a transaction.

What does that mean in simple words ?

--

However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time.

Why do you intend this ?

I don't think that the API docs are clear. Makes a n00b life miserable.

like image 780
david blaine Avatar asked Apr 17 '13 00:04

david blaine


People also ask

Why do we need transaction in Hibernate?

In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session. beginTransaction().

How does transaction management work in Hibernate?

In the hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA, JDBC). A transaction is associated with Session and instantiated by calling session.

What is transaction factory in Hibernate?

Q2 and Q3 - Yes, a hibernate transaction factory creates database transactions on underlying DB. Note that a session may be spanning over multiple transactions - A hibernate session abstracts a database connection. Over the same connection, multiple begin transaction, commit transaction cycles are possible.

What is configuration in Hibernate?

As Hibernate can operate in different environments, it requires a wide range of configuration parameters. These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file.


1 Answers

Q1 - The reasoning is clear. The transaction creation follows a factory pattern. For example, in an environment where application servers (like JBOSS, WebSphere etc) manage the transaction creation via JTA APIs, you will use a JTA Transaction factory to create transactions and that transactions will be further used by hibernate. In a purely Spring managed environment, Spring can be configured to use a HibernateTransactionFactory. In short, it would follow a similar fashion as:

IFactory f= FactoryCreatorForYourEnvironment.create();
Transaction t = f.create();

Q2 and Q3 - Yes, a hibernate transaction factory creates database transactions on underlying DB. Note that a session may be spanning over multiple transactions - A hibernate session abstracts a database connection. Over the same connection, multiple begin transaction, commit transaction cycles are possible. Example: REQUIRE_NEW properties if the participating beans (EJBs, or SPRING beans). So session is a broader (coarser) term as mentioned in the documentation.

Transactions are required for any writes, deletes, protected reads. So the session holds an implicit transaction. You can read a related stack overflow article here.

HTH.

like image 122
maggu Avatar answered Sep 20 '22 09:09

maggu