-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Under what circumstances are transactions necessary?
PostPosted: Fri May 07, 2004 12:22 pm 
Newbie

Joined: Tue May 04, 2004 11:51 am
Posts: 8
Under what circumstances are transactions necessary? I have a Customer object with a Map as an instance variable. Modifications to the map are written to the database iff I'm using transactions--is this supposed to happen?

In the following code, the attribute-value pair "COLOUR"/"BLUE" is written to the database iff the tx.commit() line is present--without it, it's not (and not error or Exception is raised).

I'm using Hibernate 2.1 and MySQL 4.0.18.

Code:
   public void testStrange() throws Exception {

      Customer c = new Customer();
      
      Map m = c.getAttribute();

      m.put("COLOUR", "BLUE");

      Session s = dowcarter.Util.getSession();

      s.save(c);

      try {

         Transaction tx = null;

         tx = s.beginTransaction();

         tx.commit();

      }
      catch (Exception e) {
         System.out.println(e);
      }
      
      s.close();

   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You always have to use transactions to encapsulate/demarcate your units of work, transactions are never ever optional (no, not even for read-only).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 12:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, the Hibernate Transaction _API_ is optional! You may manage the JDBC connection (and transaction) yourself, but the Hibernate Transaction API makes it easier to switch to JTA/container managed transactions later.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 4:56 am 
Newbie

Joined: Tue May 04, 2004 11:51 am
Posts: 8
Ah! that would explain it, thanks.

(It it possible to arrange things so that Hibernate does not "work" at all (i.e. doesn't write to database) without transactions? It's kinda perplexing to have your first few un-transactioned Hibernate experiments work fine (un-transactioned for simplicity), but the next few not work at all.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 5:04 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You definitely should get the big picture about transactions first, your description doesn't make much sense. If you don't commit a database transaction (either direct JDBC or the Hibernate API), nothing will be written to the database, as the transaction (remember, there always is one) of the connection is rolled back, as soon as the connection is closed.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:15 am 
Newbie

Joined: Tue May 04, 2004 11:51 am
Posts: 8
When using MySQL at least, data is most certainly committed to the database even when transactions are not explicitly used. The following JUnit test writes to the database:

Code:
   public static void testExample() throws Exception {
      Session sess = example.hibernate.Util.getSession();

      Customer c = new Customer();
      
      c.setFirstName("FOO");
      c.setLastName("BAR");
      
      sess.save(c);
      
      sess.close();
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Because MySQL doesn't support transactions by default! It is not a database.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.