-->
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.  [ 4 posts ] 
Author Message
 Post subject: Unable to commit in a managed transaction
PostPosted: Fri Apr 29, 2005 3:18 am 
Newbie

Joined: Fri Jan 07, 2005 3:18 pm
Posts: 16
Location: Boulder, CO
I am having trouble using Hibernate with JBoss.


I do the following in my code, which is a management method of an MBean (see code below):

1) begin a UserTransaction
2) get a session using HibernateContext
3) select a Hibernate object (in a separate method)
4) add an element to the object's collection
5) commit the transaction

I get an exception telling me that I can't commit in a managed transaction. If I omit the transaction then I get an error telling me that a Session cannot be generated outside of transaction scope.

I have configured the Hibernate and HibernateService MBeans via hibernate-service.xml and jboss-service.xml respectively in my HAR:
Code:
    <mbean code="org.jboss.hibernate.jmx.Hibernate"
           name="jboss.har:service=Hibernate">
        <attribute name="DatasourceName">java:/DevOracleDataSource</attribute>
        <attribute name="Dialect">net.sf.hibernate.dialect.OracleDialect</attribute>
        <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
        <attribute name="CacheProviderClass">net.sf.hibernate.cache.HashtableCacheProvider</attribute>
    </mbean>

    <mbean code="net.sf.hibernate.jmx.HibernateService"
           name="jboss.jca:service=HibernateFactory,name=HibernateFactory">
        <depends>jboss.jca:service=RARDeployer</depends>
        <depends>jboss.jca:service=LocalTxCM,name=DevOracleDataSource</depends>
        <!-- Make it deploy ONLY after DataSource had been started -->
        <attribute name="MapResources">ErrorSettings.hbm.xml,ErrorTextFilter.hbm.xml</attribute>
        <attribute name="JndiName">java:/hibernate/HibernateFactory</attribute>
        <attribute name="Datasource">java:/DevOracleDataSource</attribute>
        <attribute name="Dialect">net.sf.hibernate.dialect.OracleDialect</attribute>
        <attribute name="TransactionStrategy">net.sf.hibernate.transaction.JTATransactionFactory</attribute>
        <attribute name="TransactionManagerLookupStrategy">net.sf.hibernate.transaction.JBossTransactionManagerLookup</attribute>
        <attribute name="ShowSql">false</attribute>
        <attribute name="UserTransactionName">UserTransaction</attribute>
    </mbean>


My HAR looks like this:

META-INF/
META-INF/MANIFEST.MF
META-INF/hibernate-service.xml
META-INF/jboss-service.xml
com/
com/harborsideplus/
com/harborsideplus/grover/
com/harborsideplus/grover/hibernate/
com/harborsideplus/grover/hibernate/ErrorSettings.class
com/harborsideplus/grover/hibernate/ErrorTextFilter.class
ErrorSettings.hbm.xml
ErrorTextFilter.hbm.xml


Can anyone see what I'm doing wrong?

Thanks in advance for any assistance.


--James



Hibernate version: 2.1.8

Mapping documents:
Code:
<hibernate-mapping>

    <class name="com.harborsideplus.grover.hibernate.ErrorSettings"
           table="ERROR_SETTINGS">
           
        <id name="errorSettingsId"
            column="ERROR_SETTINGS_ID"
            type="string">
            <generator class="assigned"/>
        </id>
       
        <property name="blockEmail"
                  column="BLOCK_EMAIL"
                  type="boolean"/>

        <property name="timeFilterInterval"
                  column="TIME_FILTER_INTERVAL"
                  type="long"/>

        <property name="timeFilterCount"
                  column="TIME_FILTER_COUNT"
                  type="int"/>

        <set name="errorTextFilters"
             inverse="true"
             cascade="all-delete-orphan">
            <key column="ERROR_SETTINGS_ID"/>
            <one-to-many class="com.harborsideplus.grover.hibernate.ErrorTextFilter"/>
        </set>
                 
    </class>
   
</hibernate-mapping>


<hibernate-mapping>

    <class name="com.harborsideplus.grover.hibernate.ErrorTextFilter"
           table="ERROR_TEXT_FILTERS">
           
        <id name="errorTextFilterId"
            column="ERROR_TEXT_FILTER_ID"
            type="int">
            <generator class="hilo"/>
        </id>
       
        <property name="filterString"
                  column="FILTER_STRING"
                  type="string"/>

        <many-to-one name="errorSettings"
                     column="ERROR_SETTINGS_ID"
                     class="com.harborsideplus.grover.hibernate.ErrorSettings"
                     not-null="true"/>
                 
    </class>
   
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
    public void addTextFilter (String errorSettingsId,
                               String filterText)
        throws HibernateException,
               MBeanException       
    {   
        // create and begin a transaction
        UserTransaction userTransaction;
        try
        {
            userTransaction = (UserTransaction) this.jndiContext.lookup("UserTransaction");
            userTransaction.begin();
        }
        catch (Exception e)
        {
            // log the error and throw an Exception
            this.logger.error("Unable to create a UserTransaction", e);
            throw new MBeanException(e, "Unable to create a UserTransaction");
        }
           
        // open a Hibernate session
        Session hibernateSession = HibernateContext.getSession("java:/hibernate/HibernateFactory");

        // get the ErrorSettings object
        ErrorSettings errorSettings = selectErrorSettings(errorSettingsId, hibernateSession);
       
        try
        {
            // make the ErrorSettings persistent
            hibernateSession.save(errorSettings);

            // get the original set of error text filters
            Set<ErrorTextFilter> errorTextFilters = errorSettings.getErrorTextFilters();
            if (errorTextFilters == null)
            {
                errorTextFilters = new HashSet<ErrorTextFilter>(1);
            }
                   
            // create a new ErrorTextFilter for the ErrorSettings using the filter text String
            ErrorTextFilter errorTextFilter = new ErrorTextFilter(filterText, errorSettings);
           
            // add the ErrorTextFilter to the ErrorSettings object's collection
            errorSettings.getErrorTextFilters().add(errorTextFilter);
               
            // commit the transaction
            userTransaction.commit();
        }
        catch (Exception e)
        {
            try
            {
                // something went wrong, rollback the transaction (if one was started)
                if (userTransaction != null)
                {
                    userTransaction.rollback();
                }
            }
            catch (SystemException e1)
            {
                // log the error
                this.logger.error("Unable to rollback the Hibernate transaction", e1);
            }
           
            // log the error and throw an Exception
            this.logger.error("Unable to add a text filter for ID " + errorSettingsId, e);
            throw new MBeanException(e, "Unable to add a text filter for ID " + errorSettingsId);
        }
    }


Full stack trace of any exception that occurs:
01:51:21,114 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: null
01:51:21,115 ERROR [JDBCExceptionReporter] You cannot commit during a managed transaction!
01:51:21,115 ERROR [JDBCExceptionReporter] Could not save object
java.sql.SQLException: You cannot commit during a managed transaction!
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:499)
at org.jboss.resource.adapter.jdbc.WrappedConnection.commit(WrappedConnection.java:451)
at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:126)
at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2673)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2250)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2239)
at org.jboss.hibernate.session.TransactionSynch.beforeCompletion(TransactionSynch.java:55)
at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1383)
at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1075)
at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:296)
at org.jboss.tm.TxManager.commit(TxManager.java:200)
at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.commit(ServerVMClientUserTransaction.java:126)
at com.harborsideplus.grover.mbean.ErrorManager.addTextFilter(ErrorManager.java:1520)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:144)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:642)
at org.jboss.jmx.adaptor.control.Server.invokeOpByName(Server.java:236)
at org.jboss.jmx.adaptor.control.Server.invokeOp(Server.java:202)
at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.invokeOp(HtmlAdaptorServlet.java:241)
at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.processRequest(HtmlAdaptorServlet.java:79)
at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.doPost(HtmlAdaptorServlet.java:61)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:54)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:595)
01:51:21,117 WARN [TransactionSynch] Error flushing session

Name and version of the database you are using: Oracle 9


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 8:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well, two things.

In Hibernate2, the hilo generator cannot be used within JTA transactions. This has been changed in Hibernate3 to suspend the current transaction prior to generating the id. So either do not use hilo, or use Hibernate3. Another option is to get the id-generation to occur outside the current JTA transaction; check the wiki for an example of doing this via an EJB which is marked "RequiresNew".

Also, why two different session factories? Not that it maters to your issue at hand; just curious...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 1:47 pm 
Newbie

Joined: Fri Jan 07, 2005 3:18 pm
Posts: 16
Location: Boulder, CO
Steve thanks so much for your help. I have changed my generator class to "increment" and now everything appears to be working as advertised.

My understanding is that Hibernate3 isn't yet supported with JBoss 4.0.1 (or maybe it's just not supported "out of the box"), and so I've used Hibernate2. If I can use Hibernate3 with JBoss 4.0.1 then is there a document which explains the necessary steps? I assume that I'll have to add hibernate3.jar to deploy/jboss-hibernate.deployer, and probably several other configuration changes.

I had different session factories in my deployment descriptors only because I am new at this and have cobbled together my first HAR from examples from various documents which aren't in sync. What is the preferred session factory to use, or does it make any difference? Also is it required to have these two service descriptors in the HAR, hibernate-service.xml and jboss-service.xml, or can the two be combined, and if so which of the two should be used (i.e. what is the best practice, if any)?


--James


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 30, 2005 12:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I just got the green light to commit hibernate3 support in time for the upcoming 4.0.2 release.

You would be able to take the jboss-hibernate.deployer from there and move it back to 4.0.1 (replacing the one bundled with 4.0.1).

No its not as simple as simply swapping out the hibernate jar ;)

Use the HAR. You only need one service descriptor or the other; if you are using a HAR deployment use the hibernate-service.xml, otherwise use the jboss-service.xml (i.e., with the Hibernate-maintained MBean).

For HAR deployments, by far the best place to look currently is [url]http://docs.jboss.org/jbossas/jboss4guide/r2/html/ch13.html[url]

I have some documentation sitting here for the wiki about all the new stuff. I'll move that out to the JBoss wiki soon. I am also planning some more detailed stuff later.


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