I've been trying to get this simple example to work but no matter what I do it keeps complaining about autocommit being on so it can't save...I KNOW that autocommit is off because I'm explicitly setting it on the connection AND the session. But it still complains about autocommit being on...WHAT IS GOING ON??? Latest Mysql, latest JDBC drivers, JDK 1.4.2_03 on OS X. Tomcat 5.??.??
Here is the code:
Code:
try
{
javax.naming.Context ctx = new javax.naming.InitialContext();
if(ctx == null ) throw new Exception("Boom - No Context");
System.out.println("got ctx:"+ctx);
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:comp/env/jdbc/abm");
System.out.println("got ds: "+ds);
java.sql.Connection connection = ds.getConnection();
connection.setAutoCommit(false);
// -----------------------------------------------------------------------------------
{
Statement s = connection.createStatement();
int rows = s.executeUpdate("update players set first_name=\"johny\"");
System.out.println("before commit...");
connection.rollback();
System.out.println("rolled back!");
}
// -----------------------------------------------------------------------------------
Configuration instance = new Configuration();
Configuration config = instance.configure();
SessionFactory sessionFactory = config.buildSessionFactory();
System.out.println("got sessionfactory: "+sessionFactory);
Session session = sessionFactory.openSession(connection);
boolean toggleAutoCommit = session.connection().getAutoCommit();
System.out.println("toggleAutoCommit: "+toggleAutoCommit);
//if (toggleAutoCommit) session.connection().setAutoCommit(false);
session.connection().setAutoCommit(false);
connection.setAutoCommit(false);
System.out.println("connection.getAutoCommit: "+connection.getAutoCommit());
System.out.println("got session: "+session);
Transaction tx= session.beginTransaction();
System.out.println("got transaction: "+tx);
//public Player(String firstName, String lastName, int jerseyNumber) {
Player player = new Player("Mike", "McCutcheon", 21);
session.save(player);
System.out.println("saved player...");
tx.commit();
session.close();
//
//Cat princess = new Cat();
//princess.setName("Princess");
//princess.setSex('F');
//princess.setWeight(7.4f);
//
//session.save(princess);
//tx.commit();
//
//HibernateUtil.closeSession();
}
catch (HibernateException ex)
{
ex.printStackTrace();
}
catch(Throwable t)
{
t.printStackTrace();
}
Note that I explicitly try to do a transaction before I even do anything with hibernate...just to see if the connection is working...it is. Now when this runs I get:
Code:
got ctx:javax.naming.InitialContext@bab96e
got ds: org.apache.commons.dbcp.BasicDataSource@e22950
before commit...
rolled back!
Mar 3, 2004 11:46:37 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.2
Mar 3, 2004 11:46:37 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Mar 3, 2004 11:46:37 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Mar 3, 2004 11:46:37 PM net.sf.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Mar 3, 2004 11:46:37 PM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Mar 3, 2004 11:46:38 PM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: ../../hibernate/abm.hbm.xml
Mar 3, 2004 11:46:38 PM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: example.Team -> teams
Mar 3, 2004 11:46:38 PM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: example.Player -> players
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: example.Team.players -> players
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-one association property references
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Mar 3, 2004 11:46:39 PM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.MySQLDialect
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use outer join fetching: true
Mar 3, 2004 11:46:39 PM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
Mar 3, 2004 11:46:39 PM net.sf.hibernate.connection.DatasourceConnectionProvider configure
INFO: Using datasource: java:comp/env/jdbc/abm
Mar 3, 2004 11:46:39 PM net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use scrollable result sets: true
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use JDBC3 getGeneratedKeys(): true
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: false
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: echoing all SQL to stdout
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: cache provider: net.sf.ehcache.hibernate.Provider
Mar 3, 2004 11:46:39 PM net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
Mar 3, 2004 11:46:39 PM net.sf.hibernate.impl.SessionImpl finalize
WARNING: afterTransactionCompletion() was never called
Mar 3, 2004 11:46:39 PM net.sf.hibernate.impl.SessionImpl finalize
WARNING: unclosed connection
Mar 3, 2004 11:46:40 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Mar 3, 2004 11:46:41 PM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: no JNDI name configured
got sessionfactory: net.sf.hibernate.impl.SessionFactoryImpl@257ce8
toggleAutoCommit: false
connection.getAutoCommit: false
got session: net.sf.hibernate.impl.SessionImpl@5df7c6
got transaction: net.sf.hibernate.transaction.JDBCTransaction@a81e44
Mar 3, 2004 11:46:41 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: S1000
Mar 3, 2004 11:46:41 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Can't call commit when autocommit=true
Mar 3, 2004 11:46:41 PM net.sf.hibernate.JDBCException <init>
SEVERE: Could not save object
java.sql.SQLException: Can't call commit when autocommit=true
at com.mysql.jdbc.Connection.commit(Connection.java:1094)
at org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:248)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.commit(PoolingDataSource.java:244)
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:747)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
at example.HibernateTest.doGet(HibernateTest.java:88)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:552)
net.sf.hibernate.JDBCException: Could not save object
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:765)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
at example.HibernateTest.doGet(HibernateTest.java:88)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:552)
Caused by: java.sql.SQLException: Can't call commit when autocommit=true
at com.mysql.jdbc.Connection.commit(Connection.java:1094)
at org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:248)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.commit(PoolingDataSource.java:244)
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:747)
... 28 more
What in the world am I doing wrong?
Thanks,
Mike