-->
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: Can't drop MySQL database from within session bean
PostPosted: Fri Feb 13, 2004 1:51 am 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
My test code relies pretty heavily on dropping the database and recreating it. Up until now I have been using hibernate.hbm2ddl.auto=create-drop. That is no longer the best thing as I need more control over when this happens (shouldn't just happen every time Hibernate starts up -- should only happen when I run a test case that explicitly drops the database).

So I have a simple session bean whose method just does:

Code:
    /**
     * FOR TESTING ONLY, DUH!!!  Drop all database tables!
     * @ejb:interface-method view-type="both"
     */
    public void dropDatabase () {
        try {
            Persistence.dropDatabase();
        } catch (Exception ex) {
            throw new NFInternalErrorException("Could not drop and create database", ex);
        }
    }


This works great with Postgres. With MySQL, though, I get this:

Code:
2004-02-12 21:46:49,902 : SchemaExport.execute : Running hbm2ddl schema export
2004-02-12 21:46:49,902 : SchemaExport.execute : exporting generated schema to database
2004-02-12 21:46:49,918 : NamingHelper.getInitialContext : JNDI InitialContext properties:{}
2004-02-12 21:46:49,918 : DatasourceConnectionProvider.configure : Using datasource: jdbc_1
2004-02-12 21:46:50,183 : SchemaExport.execute : schema export complete
2004-02-12 21:46:50,183 : XAResourceImpl.commit : Cannot commit transaction:java.sql.SQLException: C
an't call commit when autocommit=true
2004-02-12 21:46:50,183 : Pool.closeConnection : connection was already closed
2004-02-12 21:46:50,183 : TransactionImpl.commit : Unexpected Exception on commit_one_phase:
java.rmi.RemoteException: XAException:javax.transaction.xa.XAException: Error on commit
        at org.objectweb.jonas_tm.SubCoordinator.doOnePhaseCommit(SubCoordinator.java:687)
        at org.objectweb.jonas_tm.SubCoordinator.commit_one_phase(SubCoordinator.java:273)
        at org.objectweb.jonas_tm.TransactionImpl.commit(TransactionImpl.java:187)
        at org.objectweb.jonas_tm.Current.commit(Current.java:237)
        at org.objectweb.jonas_ejb.container.JFactory.postInvoke(JFactory.java:400)
        at org.objectweb.jonas_ejb.container.JFactory.postInvokeRemote(JFactory.java:504)
        at org.objectweb.jonas_ejb.container.JSessionRemote.postInvoke(JSessionRemote.java:169)
        at com.nimblefish.sdk.ejb.sessioninterfaces.JOnASClientCampaignServiceRemote.dropDatabase(JO
nASClientCampaignServiceRemote.java:133)
        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:324)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
        at org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef.runDispatch(JUnicastServerRef.java:
166)
        at org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef.dispatch(JUnicastServerRef.java:150
)
        at sun.rmi.transport.Transport$1.run(Transport.java:148)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
        at java.lang.Thread.run(Thread.java:534)
2004-02-12 21:46:50,246 : JFactory.postInvoke : ejbexception:
javax.ejb.EJBException: Container exception; nested exception is: javax.transaction.SystemException:
Unexpected Exception on commit_one_phase
javax.transaction.SystemException: Unexpected Exception on commit_one_phase
        at org.objectweb.jonas_tm.TransactionImpl.commit(TransactionImpl.java:193)
        at org.objectweb.jonas_tm.Current.commit(Current.java:237)
        at org.objectweb.jonas_ejb.container.JFactory.postInvoke(JFactory.java:400)
        at org.objectweb.jonas_ejb.container.JFactory.postInvokeRemote(JFactory.java:504)
        at org.objectweb.jonas_ejb.container.JSessionRemote.postInvoke(JSessionRemote.java:169)
        at com.nimblefish.sdk.ejb.sessioninterfaces.JOnASClientCampaignServiceRemote.dropDatabase(JO
nASClientCampaignServiceRemote.java:133)
        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:324)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
        at org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef.runDispatch(JUnicastServerRef.java:
166)
        at org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef.dispatch(JUnicastServerRef.java:150
)
        at sun.rmi.transport.Transport$1.run(Transport.java:148)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
        at java.lang.Thread.run(Thread.java:534)


What is the bad interaction here between autocommit (apparently) and the EJB container's transaction handling? What is the right way to address it? And why only with MySQL?

Thanks to you all incredibly as always....
Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 2:01 am 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
I tried adding
Code:
hibernate.connection.autocommit     false

to my hibernate properties for MySQL. Didn't help. Also tried adding
Code:
jdbc.autocommit=false

to the app server's datasource properties. Also didn't help.

Help?
Thanks :-)
Cheers --
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 2:07 am 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Finally, note that I *am* running with InnoDB tables (configured as such in the [mysqld] default configuration)....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 2:14 am 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
And finally finally (sorry I didn't just say all this up front!), I am using the mysql-connector-java-3.0.9-stable-bin.jar driver.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 2:28 am 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Duh. Fixed it. Just do this:
Code:
    /**
     * FOR TESTING ONLY, DUH!!!  Drop all database tables!
     * @ejb:interface-method view-type="both"
     * @ejb:transaction type="NotSupported"
     */
    public void dropDatabase () {
        try {
            Persistence.dropDatabase();
        } catch (Exception ex) {
            throw new NFInternalErrorException("Could not drop and create database", ex);
        }
    }


If the transaction is causing problems, then DON'T USE THE TRANSACTION!!!

("Doctor, it hurts when I do this." "Well, DON'T DO THAT!")

Thanks for your patience.
Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 9:19 am 
Beginner
Beginner

Joined: Thu Jan 15, 2004 11:16 am
Posts: 37
Would you mind adding the code you use to drop and create the database in tests?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 12:25 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Well, now we do it in an Ant task -- we have refactored our tests to avoid relying on an empty database (since that means multiple people can never run concurrent tests on a shared database).

The Ant task we use is:

Code:
    <target name="recreate_database" depends="configure,compile"
        description="Recreate the database for the specified environment.">

        <echo message="Dropping and recreating database using configuration ${barracuda.env}.cfg.xml..."/>

        <schemaexport
            config="/${barracuda.env}.cfg.xml"
            quiet="true"
            drop="true"
            delimiter=";"/>

        <schemaexport
            config="/${barracuda.env}.cfg.xml"
            quiet="true"
            delimiter=";"
            output="${build.dir}/schema_export.sql"/>

        <echo message="Done; see sql in ${build.dir}/schema_export.sql"/>

    </target>


Works great, and gives us better control over wiping the database only when we need to.

The code for doing it is

Code:
    public static void createDatabase () throws NfPersistenceException {
        log.debug("About to create database...");
        try {
            new SchemaExport(configuration).create(true, true);
            log.debug("Done.");
        } catch (Exception e) {
            throw new NfPersistenceException("Could not drop database", e);
        }
    }

    /**
     * Drop the database.
     * @throws NfPersistenceException if db error
     */
    public static void dropDatabase () throws NfPersistenceException {
        log.debug("About to drop database...");
        try {
            new SchemaExport(configuration).drop(true, true);
            log.debug("Done.");
        } catch (Exception e) {
            throw new NfPersistenceException("Could not drop database", e);
        }
    }


where "configuration" is, of course, a net.sf.hibernate.config.Configuration.

Cheers!
Rob


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.