Hi there,
I am running hibernate 2.1 with hypersonic in jboss 3.2.3 . I have an object which has 2 many to one relationships that occasionally gets the following exception:
10:43:10,883 WARN [JDBCExceptionReporter] SQL Error: -8, SQLState: 23000 10:43:10,883 ERROR [JDBCExceptionReporter] Integrity constraint violation: FKCE9643CFE87FB7F1 table: MM_PRICE in statement [insert into MM_ORDER (PRICE_ID, ACCOUNT_ID, amount, stopLoss, quantity, id) values (569, 274, 100, 90, 100, 569)] 10:43:10,898 ERROR [JDBCExceptionReporter] could not insert: [com.mm.data.Order#569] java.sql.SQLException: Integrity constraint violation: FKCE9643CFE87FB7F1 table: MM_PRICE in statement [insert into MM_ORDER (PRICE_ID, ACCOUNT_ID, amount, stopLoss, quantity, id) values (569, 274, 100, 90, 100, 569)] at org.hsqldb.Trace.getError(Unknown Source) at org.hsqldb.Result.<init>(Unknown Source) at org.hsqldb.jdbcConnection.executeHSQL(Unknown Source) at org.hsqldb.jdbcConnection.execute(Unknown Source) at org.hsqldb.jdbcStatement.fetchResult(Unknown Source) at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source) at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source) at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:324) at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:469) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:443) at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2261) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187) at net.sf.hibernate.transaction.JTATransaction.commit(JTATransaction.java:52) at com.mm.dataservice.PersistenceManagerBean.save(PersistenceManagerBean.java:42) at com.mm.dataservice.PersistenceManagerBean.onMessage(PersistenceManagerBean.java:25) at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContainer.java:460) at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185) at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:62) at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84) at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267) at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128) at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:90) at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191) at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122) at org.jboss.ejb.MessageDrivenContainer.internalInvoke(MessageDrivenContainer.java:374) at org.jboss.ejb.Container.invoke(Container.java:700) at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:824) at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1114) at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:256) at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:633) at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:433) at org.jboss.mq.SpySession.run(SpySession.java:298) at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:180) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:732) at java.lang.Thread.run(Thread.java:534) 10:43:11,195 ERROR [SessionImpl] Could not synchronize database state with session net.sf.hibernate.JDBCException: could not insert: [com.mm.data.Order#569] at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:479) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:443) at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2261) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187) at net.sf.hibernate.transaction.JTATransaction.commit(JTATransaction.java:52) at com.mm.dataservice.PersistenceManagerBean.save(PersistenceManagerBean.java:42) at com.mm.dataservice.PersistenceManagerBean.onMessage(PersistenceManagerBean.java:25) at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContainer.java:460) at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185) at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:62) at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84) at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267) at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128) at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:90) at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191) at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122) at org.jboss.ejb.MessageDrivenContainer.internalInvoke(MessageDrivenContainer.java:374) at org.jboss.ejb.Container.invoke(Container.java:700) at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:824) at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1114) at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:256) at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:633) at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:433) at org.jboss.mq.SpySession.run(SpySession.java:298) at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:180) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:732) at java.lang.Thread.run(Thread.java:534) Caused by: java.sql.SQLException: Integrity constraint violation: FKCE9643CFE87FB7F1 table: MM_PRICE in statement [insert into MM_ORDER (PRICE_ID, ACCOUNT_ID, amount, stopLoss, quantity, id) values (569, 274, 100, 90, 100, 569)] at org.hsqldb.Trace.getError(Unknown Source) at org.hsqldb.Result.<init>(Unknown Source) at org.hsqldb.jdbcConnection.executeHSQL(Unknown Source) at org.hsqldb.jdbcConnection.execute(Unknown Source) at org.hsqldb.jdbcStatement.fetchResult(Unknown Source) at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source) at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source) at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:324) at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:469) ... 31 more 10:43:11,570 ERROR [STDERR] net.sf.hibernate.JDBCException: could not insert: [com.mm.data.Order#569] at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:479) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:443) at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2261) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187) at net.sf.hibernate.transaction.JTATransaction.commit(JTATransaction.java:52) at com.mm.dataservice.PersistenceManagerBean.save(PersistenceManagerBean.java:42) at com.mm.dataservice.PersistenceManagerBean.onMessage(PersistenceManagerBean.java:25) at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContainer.java:460) at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185) at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:62) at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84) at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267) at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128) at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:90) at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191) at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122) at org.jboss.ejb.MessageDrivenContainer.internalInvoke(MessageDrivenContainer.java:374) at org.jboss.ejb.Container.invoke(Container.java:700) at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:824) at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1114) at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:256) at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:633) at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:433) at org.jboss.mq.SpySession.run(SpySession.java:298) at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:180) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:732) at java.lang.Thread.run(Thread.java:534) Caused by: java.sql.SQLException: Integrity constraint violation: FKCE9643CFE87FB7F1 table: MM_PRICE in statement [insert into MM_ORDER (PRICE_ID, ACCOUNT_ID, amount, stopLoss, quantity, id) values (569, 274, 100, 90, 100, 569)] at org.hsqldb.Trace.getError(Unknown Source) at org.hsqldb.Result.<init>(Unknown Source) at org.hsqldb.jdbcConnection.executeHSQL(Unknown Source) at org.hsqldb.jdbcConnection.execute(Unknown Source) at org.hsqldb.jdbcStatement.fetchResult(Unknown Source) at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source) at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source) at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:324) at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:469) ... 31 more
For persistence, I have a MDB that listens on a topic, and persists things as they come. The code is as follows -
import com.mm.util.ApplicationToolbox; import net.sf.hibernate.*;
import javax.ejb.EJBException; import javax.ejb.MessageDrivenBean; import javax.ejb.MessageDrivenContext; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.naming.Context; import javax.naming.NamingException;
public class PersistenceManagerBean implements MessageDrivenBean, MessageListener { public MessageDrivenContext messageDrivenContext; private SessionFactory sessionFactory = null; private Context jndiContext;
public void onMessage(Message message) { try { ObjectMessage objectMessage = (ObjectMessage) message; save(objectMessage.getObject()); } catch (JMSException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
public Object save(Object object) throws Exception { Object ret = null; Session sess = null; net.sf.hibernate.Transaction transaction = null; try { sess = getSession(); transaction = sess.beginTransaction(); ret = sess.save(object); transaction.commit(); } catch (Exception exception) { rollbackTransaction(transaction); throw exception; } finally { closeSession(sess); } return ret; }
public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) throws EJBException { this.messageDrivenContext = messageDrivenContext; }
private Session getSession() throws NamingException, HibernateException { Session session = getSessionFactory().openSession(); session.setFlushMode(FlushMode.COMMIT); return session; }
private void setJndiContext() { try { jndiContext = ApplicationToolbox.getInstance().getInitialContext(); } catch (NamingException e) { e.printStackTrace(); } }
private SessionFactory getSessionFactory() throws NamingException { if (sessionFactory == null) { sessionFactory = (SessionFactory) jndiContext.lookup("java:/hibernate/HibernateFactory"); } return sessionFactory; }
private void closeSession(Session sess) { try { if (sess != null) { sess.close(); } } catch (HibernateException e) { e.printStackTrace(); } }
private void rollbackTransaction(net.sf.hibernate.Transaction transaction) { try { if (transaction != null) { transaction.rollback(); } } catch (HibernateException e1) { e1.printStackTrace(); } }
public void ejbCreate() { setJndiContext(); }
public void ejbRemove() { } }
The reason the exception is thrown is because the first object (Order) tries to save itself to the database before the second object's (Price) session has finished executing. Is there some way that I can overcome this by relaxing the foreign key constraints... Or do I need to catch the exception, and try to insert again..or have i mplemented something incorrectly?
Thanks in Advance, Dave_Dave
|