Java
  Home arrow Java arrow Page 2 - Managing Transactions with Hibernate
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Managing Transactions with Hibernate
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 40
    2006-04-19

    Table of Contents:
  • Managing Transactions with Hibernate
  • Transactions: Core APIs
  • Hibernate in the Real World: Implementing a Utility class for Hibernate
  • Implementing a Utility class for Hibernate continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Managing Transactions with Hibernate - Transactions: Core APIs


    (Page 2 of 4 )

    The core of transactions in Hibernate is the transaction interface. This interface is implemented to provide three of the in-built classes that do the real work(communication and transaction management at database/JTA level). A transaction object is always associated with a session object. To instantiate a transaction object, beginTransaction() is called on the session object. For example if session is the session object then:

    Transaction tcx=session.beginTransaction();

    The above statement would instantiate a transaction object represented by tcx. Many instances of transaction can be retrieved using same session object as a session (not class) can span many transactions (not classes). There are three implementations of the transaction interface provided by Hibernate. They are:

    a. JDBCTransaction

    b. JTATransaction

    c. CMTTransaction

    Of these, JDBCTransaction is the default. That means if hibernate.transaction_factory key’s value is not set in the hibernate.properties file, the transaction object returned by the beginTransaction() method is the object of the JDBCTransaction class. The main methods of the transaction interface are commit() and rollback().

    Since many transactions can take place within a conversation, the concept of savepoints is not required. The functionality is provided by the concept of multiple transactions within the session. Here are the details of the methods:

    Commit() is one of the two methods used to end a transaction. It also flushes the session associated with it. Commit() internally commits the underlying transaction. So just calling the save() method on the session object wouldn’t persist (save) the data contained in the ORM object into the database.  One more point to keep in mind is that the underlying transaction is committed if and only if the transaction was initiated by the calling object.

    Rollback(), as its name suggests, rolls back the underlying transaction. So to start a transaction and then commit it, the statements would be:

    Transaction tcx=session.beginTransaction();
    //..save, update, delete retrieve  statements
    tcx.commit();

    That’s all you need to know about methods of transaction. In the next section I will be discussing the thread-local pattern and how to use it to create a Factory class that would encapsulate most of the initialization steps in using Hibernate.

    More Java Articles
    More By A.P.Rajshekhar


       · Transaction control is the base of database based programming where concurrent...
       · Dear Sir/ Madam, I am using the same pattern for transaction commit, when I keep...
       · Hi:I understand how you are obtaining and closing the session, but what it is...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek