Alphalearningschool
        

<<< Prev

Next >>>

Up
Hibernate Transaction Management

Tx Management: Transaction is an interface available in org.hibernate package which is associated with the session.

In the transaction, if any single step fails, the complete transaction will be failed.

We can describe transaction with ACID properties.

Atomicity – All success or none.

Consistency – Database constraints should not be violated.

Isolation – One transaction should not effect another one.

Durability – It should in Database after commit.

Let’s see an example to get Transaction object

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
 
public class TransactionExm {
 
	public static void main(String[] args) {
		
		SessionFactory factory = null;
		
		Session session = null;
		
		Configuration configuration = new Configuration().configure();
		
		try {
			
			factory = configuration.buildSessionFactory();
			
			session = factory.openSession();
			
			Transaction tx = session.beginTransaction();
			
			System.out.println("transaction object" + tx);
			
			tx.commit();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
 
		} finally {
 
			session.close();
			
			factory.close();
			
		}
		
	}
	
}