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