• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is Transaction in hibernate

 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use of Transaction in hibernate
 
Devaka Cooray
Marshal
Pie
Posts: 4886
406
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aruna sameera wrote:what is the use of Transaction in hibernate

Transactions can be used to prevent inappropriate usage of resources. Transactions-handling is an interesting topic on EJB development.
Please read this Sun Tutorial.

hth

Devaka.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A transaction represents a unit of work. If you want the database to do some work for you, then you need to start a transaction. When you are finished interacting with the database, you end the transaction, and let the database know you are done.

-Cameron McKenzie
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found this tutorial.But in this tutorial when saving data they didn't use transaction.they use Transaction for updating.
here ARE THE EXAMPLES.

[codeE]

package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}

[/code]

 
Lincov Silva
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate transaction is linked with session http://learnfromneo.blogspot.com/2009/06/what-is-hibernate-session.html

Hibernate has its own transaction class and you can get it through session interface.

Within one transaction you can do several operations and can commit transaction once after successfully completed all operations. The advantage here is you can rollback all previous operations if one operation is fail in your operation batch.

Using transaction is not must. Your transaction will commit when you call session.flush().

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lincov Silva wrote:
Your transaction will commit when you call session.flush().


The Transaction does not get committed when session gets flushed.

When someone flushes the session, it means that the current changes are made to the db(ie the queries are executed) so that these changes are visible to this application if it tries to get a related data from the DB.
One can always rollback the transaction after flush()

Changes are committed when the tx.commit() is called or when the session closes.

It is a good practice to make changes in a Transaction and not rely on the session for this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic