-->
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.  [ 14 posts ] 
Author Message
 Post subject: cannot commit during managed transaction problem
PostPosted: Sun Oct 31, 2004 5:41 pm 
Newbie

Joined: Sun Oct 31, 2004 5:31 pm
Posts: 1
I have a simple object that I am trying to save. I get a JBoss exception as shown below. It must have something to do with using the Hibernate hilo id generator. Any ideas?

JBoss4 throws :

2004-10-31 16:39:36,323 WARN [net.sf.hibernate.util.JDBCExceptionReporter] SQL Error: 0, SQLState: null
2004-10-31 16:39:36,323 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] You cannot commit during a managed transaction!
2004-10-31 16:39:36,326 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] Could not save object
java.sql.SQLException: You cannot commit during a managed transaction!


code:
Code:
         Transaction tx = session.beginTransaction();
         
         QuoteUser qu = new QuoteUser();
         qu.setPassword("patrick");
         qu.setUsername("patrick");
         session.save(qu);
            
         
         tx.commit();   
         
         session.close();


Mapping:
Code:
<hibernate-mapping>
    <class name="net.pwb.quoteserver.vo.QuoteUser"
           table="quoteuser">
             
        <id name="userid" type="long"
            unsaved-value="null">
            <column name="userid" sql-type="integer"
                    not-null="true"/>
            <generator class="hilo"/>
        </id>
        <property name="username">
            <column name="username" sql-type="char(25)"
                    not-null="true"/>
        </property>
        <property name="password">
            <column name="password" sql-type="char(25)"
                    not-null="true"/>
        </property>
               
    </class>
</hibernate-mapping>
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 01, 2004 6:06 am 
Newbie

Joined: Mon Nov 01, 2004 5:39 am
Posts: 2
You're trying to commit from within a bean running in a managed environment (ie JBoss).

You need to change the transaction strategy so enable HB integration with JTA

In hibernate.properties, change the factory_class property

hibernate.transaction.factory_class=net.sf.hibernate.transaction.JTATransactionFactory

Look into Hibernate in Action, Chapter 2, section 2.3.3 if you need further information


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 01, 2004 7:00 am 
Newbie

Joined: Mon Nov 01, 2004 5:39 am
Posts: 2
oops, sorry looks like this is what you're looking for

http://forum.hibernate.org/viewtopic.ph ... ransaction


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 21, 2004 8:16 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 12:45 pm
Posts: 43
Location: Israel
ltexier wrote:
You're trying to commit from within a bean running in a managed environment (ie JBoss).

You need to change the transaction strategy so enable HB integration with JTA


You cannot use HiLo when you get the connection from a Datasource. the reason is that the Hilo explictly use connection.commit(); and it is not allowed when you get a connection from a Datasource.

It will not help if you will use JTA.

You need to get the ID in a way that it won't need to commit (Identity, Sequence), or use Stateless-Session-Bean that will generate the ID for you (remember to mark the EJB transaction mode to "not supported".

Hope it helps.
Jus.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 29, 2004 4:11 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
While reading the comments in the TableHiLo Generator class, I saw the following

"[i]This has to be done using a different connection to the containing transaction because the new hi value must remain valid even if the containing transaction rolls back[/i]"

Is this related to the fact that you cannot use HiLo in this specific case?

Also to get around this, I'm going to write a session bean to generate HiLo identifiers, but since I don't have much experience with EJBs, I would appreciate some pointers on where or how to start, e.g. what should I be doing first. I'm using Jonas app server if that helps.
Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 29, 2004 4:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I believe there is a pattern in our wiki.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 29, 2004 6:12 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
I've looked at the pattern at http://www.hibernate.org/47.html and I'm not understanding the purpose of the SessionBeanHiLoGenerator class. It seems to be redundant.

It seems that HiLoGeneratorBean calls the generate method of the TableHiLoGenerator class provided by Hibernate. So, essentially we wrap ALL the functionality of the TableHiLoGenerator, and its associated classes into this HiLoGeneratorBean session bean. All we need to do is to create a client for the HiLoGeneratorBean class, e.g. HiLoClient, which will call the next method in the session bean. Am I correct in my assumption, or have I missed something. Please excuse my ignorance as I've only started learning EJBs now, and am not very knowledgeable on them.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 29, 2004 6:36 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2004 12:45 pm
Posts: 43
Location: Israel
acha114 wrote:
I'm not understanding the purpose of the SessionBeanHiLoGenerator class. It seems to be redundant.

All we need to do is to create a client for the HiLoGeneratorBean class, e.g. HiLoClient, which will call the next method in the session bean.



the SessionBeanHiLoGenerator is the HiLoClient you are talking about.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 29, 2004 8:18 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
[quote="Issahar"][quote="acha114"]I'm not understanding the purpose of the SessionBeanHiLoGenerator class. It seems to be redundant.

All we need to do is to create a client for the HiLoGeneratorBean class, e.g. HiLoClient, which will call the next method in the session bean.
[/quote]


the [b]SessionBeanHiLoGenerator[/b] is the [b]HiLoClient[/b] you are talking about.[/quote]

Hi, thanks for clarifying that. In the SessionBeanHiLoGenerator class, where does the HiLoGeneratorUtil class come from? What function does that serve? Is there any code floating around for this?

What I've done is created a HiLo client with a main() method that interfaces with the HiLoGenerator Bean class. I still haven't got that to work yet, but I suppose this is an alternative approach, right?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 3:19 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 12:45 pm
Posts: 43
Location: Israel
Hi.

You need to create a class that implements IdentifierGenerator. This is the class that should be written in the hbm file (in the wiki example, it is SessionBeanHiLoGenerator). This is the class that will be called by Hibernate when trying to get a new ID.

Now, this class can't use the same managed connection due to the things we talked above, so it calls a SSB, which uses a different connection, and doesn't participate in the transaction that Hibernate is in right now (notice the @ejb.transaction type="NotSupported". it means that the EJB is NOT within the transaction that called it).

I think that the HiLoGeneratorUtil is a helper class not listed in the example. It just gets the home interface for the SSB (EJB stuff).

I don't know what you need the main() in your class for.

Jus.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 5:03 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
Hi thanks for your help Issahar.

I wrote a standalone app client with a public static void main method (e.g. HiloMain.java) and it is just used to test that my session bean works as expected. It generates unique hilo values every time I make a call to the session bean from inside the main method.

I can see that using HiloMain.java is probably the same as that SessionBeanHiLoGenerator example, however this time instead of running it as a standalone application, I have to call SessionBeanHiLoGenerator in my hbm files.

I'm still just a little unclear understanding the issue between individual transactions though. I know that hilo needs a separate transaction to fetch hi values from the database, but how does wrapping it in a session bean help here???

Thanks for your kind help and sorry for my ignorance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 6:52 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2004 12:45 pm
Posts: 43
Location: Israel
acha114 wrote:
but how does wrapping it in a session bean help here???


JTA means that the container wraps all the code running in one method with a transaction, and even the code that this method calls to (even in other EJBs). If the EJB that is being called states that it doesn't want to be in your transaction - the container suspends the caller's transaction, and create a new transaction for the method in the second EJB.
That's what the example did. it stated "notSupported" in the second EJB, so that its code is in a different transaction than the one calling it.
That's why you need the id generation in a SSB, with transaction "notSupported".

Jus.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 4:21 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
In the private SessionImplementor getSession() method, Im not exactly sure what goes here.

is it something like:
.....
Session s = factory.openSession();
return (SessionImplementor)s;

Or is there something more?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 3:33 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 12:45 pm
Posts: 43
Location: Israel
Maybe it's just getting the sessionFactory from the jndi, but i think it doesn't affect the JTA.
Think so.
Jus.


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