-->
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.  [ 10 posts ] 
Author Message
 Post subject: Save works only within Transaction
PostPosted: Fri Feb 25, 2005 7:02 am 
Newbie

Joined: Fri Feb 25, 2005 6:45 am
Posts: 3
When I Session.save(object) an object not using transactions it is not stored in the database;

When I surround the save procedure with a hibernate transaction it is stored.

Can someone give me a hint, where the problem might be?

btw: there is no exception, and Hibernate sends back the number of the generated ID...

thank you


Alex




Hibernate version: 2.1.8

Mapping documents:

<hibernate-mapping>
<class name="info.schatten.hibernatetest.data.Lva" table="LVA">
<id name="idx" column="idx" type="long">
<generator class="identity"/>
</id>
<property
name="nr"
column="nr"
type="integer"
not-null="true"
/>
<property
name="typ"
column="typ"
type="string"
length="2"
not-null="true"
/>
<property
name="titel"
column="titel"
type="string"
length="50"
not-null="true"
/>
<set name="abgehalten">
<key column="lva_ref" />
<one-to-many class="info.schatten.hibernatetest.data.Abgehalten" />
</set>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Lva lva = new Lva();
va.setNr(123);
lva.setTitel("Test");
lva.setTyp("UE");
generatedID = (Long)session.save(lva);


Full stack trace of any exception that occurs:

no exception

Name and version of the database you are using:

hsqldb 1.8 RC2

The generated SQL (show_sql=true):


Hibernate: insert into LVA (nr, typ, titel, idx) values (?, ?, ?, null)
Hibernate: call identity()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 9:35 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Ahem..... What seems to be the problem?

How can you save and object and expect it to appear in the database without committing your save?

You've probably grown used to the autocommit flag being set to 'true' on the java.sql.Connection object (the default value) and therefore have assumed that an insert does not require a transaction. It always does - just that it's done for you in this case.


Top
 Profile  
 
 Post subject: Following the F.A.Q
PostPosted: Fri Feb 25, 2005 10:05 am 
Newbie

Joined: Fri Feb 25, 2005 6:45 am
Posts: 3
The F.A.Q. (and also the documentation) says:

"Hibernate will not execute any SQL until you either call Transaction.commit() (if using the Transaction API) or Session.flush() (if you are not) at the end of a session to flush your in-memory changes to the database. "


As I need no transactions in the application I just used the session.save and flush (or session.close).

But the data is not saved! Only when using Transactions. but this is (following the documentation) not required!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 10:10 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
No - you need transactions.

Executing a sql statement and commiting a transaction are two very different concerns.

Quote:
but this is (following the documentation) not required!

Where exactly does it say you don't need transactions?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 10:17 am 
Newbie

Joined: Fri Feb 25, 2005 6:45 am
Posts: 3
Thank you.

I was confused by the recommended tutorial from "SystemMobile"... which is actually more confusing than helpful after all;

I learned much faster and better using the reference documentation (which is very good, btw)...


Top
 Profile  
 
 Post subject: not using transactions
PostPosted: Wed Mar 02, 2005 2:47 pm 
Newbie

Joined: Wed Mar 02, 2005 1:28 pm
Posts: 4
The application Im working with does not use transactions (it uses <property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>)
And the objects are saved by simply doing a flush (the definition of flush I found on one of the books Ive been working with is something like: to update the database with all current persistent objects)
The only thing I dont like is that I cannot rollback (only transactions rollback) so on exceptions all I can do is close the session. Does anyone know if a rollback would be required if in the middle of trying to update some object I get an exception. Id appreciate it if somebody could enlighten me on this subject.
I'm new to hibernate so if any of the things I said are completely wrong Id appreciate the help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 9:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
A CMT/JTA environment can be rolled back. You need to provide the rollback hint to the context and it will be done. Refer to a standard EJB or JTA book for further information.


Top
 Profile  
 
 Post subject: still have small doubts
PostPosted: Thu Mar 03, 2005 12:08 pm 
Newbie

Joined: Wed Mar 02, 2005 1:28 pm
Posts: 4
Ok, I quickly browsed the Java transactions API, but JTA still uses transactions. My doubts lie in the following piece of code:

Session session = hibernate.getSession(hibernate.DB2);
session.saveOrUpdate(objectToPersist);
session.flush();
session.close();

The code works; but what if some kind of problem occurs in the middle of the db transaction. Since Im not using an explicit transaction, I cannot rollback right?
Also, is it good practice to do this, or should I create a transaction first and then persist data using that transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 12:16 pm 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
It's good practice to do as follows for inserts and updates :

Transaction tx = null;
try {
sess = hibSessionFactory.openSession();
tx = sess.beginTransaction();
......................
} catch (Exception e) {
try {
tx.rollback();
} catch (HibernateException e1) {
// do what you want to do for such case
}
throw new AppException(e);
} finally {
if (sess != null) {
try {
sess.close();
} catch (HibernateException e) {
// do what you want to do for such case
}
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 9:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Here is an example (well sort of).

Setup - you have a SLSB using CMT with a method on it that has 'Required' transaction semantics, see below doSomething().


Code:
public class TestBean implements javax.ejb.SessionBean {
     private SessionContext ctx;

     /// .. All the create etc routines.

     public void setSessionContext(javax.ejb.SessionContext ctx) {
           this.ctx = ctx;
     }

     // Business methods - with TX semantics already started so just load session and go
     public String doSomething() {
            String retVal;
            Session sess;
            try {
                   // ... Do you code here no Tx stuff necessary
                   sess = HibernateUtil.getSession();
                   sess.save(new X());
                   retVal = "Yes it worked";
            }               
            catch (Exception ex) {
                    // Do something appropriate then rollback
                    ctx.setRollbackOnly();
                     retVal = "No it was rolled back";
            }
            HibernateUtil.closeSession(sess);
            return retVal;
     }

}


When the execution entered the doSomething method a CMT TX is started.
See how the SessionContext is used to tell (hint) that it CMT needs to rollback the CMT transaction. See

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/ejb/SessionContext.html
For more details on the SessionContext.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.