-->
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.  [ 14 posts ] 
Author Message
 Post subject: Problem with flush and commit
PostPosted: Thu Feb 23, 2006 7:26 pm 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
Hibernate version:
3.1.2

I have a table with a database trigger that fires on inserts and updates. When I insert a record using save on the Session object I then explicitly call flush to fire the trigger. If the trigger raises an exception in the database I get a constraint violation exception which is what I want.

However then when I call commit later in my code, hibernate tries to write the same object again and throws the same exception.

I have tried calling evict to remove the object from the session after the call to flush but this hasnt had any affect. I still want to continue the transaction.

I have flush_before_completion set to true which is the behaviour I want for the other tables. Setting it to false fixes the problem but isnt the behaviour I want at a higher level.

I guess my question is how do I either remove it from the session after a flush or how do I stop commit from writing it when I dont want it to.


Code:
    public PersonTO insert(PersonTO person) throws DAOException {
        try {
            session.save(person);

            session.flush();
           
            return person;
      } catch(HibernateException e) {
                    try {
                        session.evict(person);
                    } catch(HibernateException he) {
                        throw new DAOException(he);
                    }
         throw new DAOException(e);
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 7:54 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
Don't worry about "flush" you'll not need to call it.

Just use save and commit.

In a basic sense think of Hibernate as a cache of database objects
for the duration of your session. If you call evict, then it will clear the cache for that object or set of objects, again in hibernate.

Flush will happen automatically on commit and sometimes before selecting info from the DB (if setup to do so).

In this case the DB trigger should fire upon your commit call from hibernate.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:01 pm 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
I need to call flush because I want to trigger the logic in the trigger at the point when I call insert in my code. I cant have it happening when the container commits the transaction as its too late to handle it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:20 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
DB's dont' work that way - they fire triggers upon committing - at least that is my experience in hib. Perhaps you'd like to look at Hibernate's Interceptor for what you want?


Last edited by jt_1000 on Fri Feb 24, 2006 11:20 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:31 pm 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
DB's do fire triggers upon committing. At the moment the trigger is firing at flush and is also firing at commit. I dont want hibernate to send the insert statement again at commit. I want to be able to remove the object from the sesssion so its not there at commit. How can I do this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:41 pm 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
Scrap what I just said in the first sentence major type there. DBs do fire triggers at each SQL statement. Hibernate makes it looks like its happening at commit cos it calls flush at commit which will cause the SQL statements to be sent to the database thus invoking the trigger.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:46 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
1) in my opinion - you are messing with transactional semantics in a weird way.

transactions work like this:

start TRX
insert ...
insert...
update...
commit. //which ends the transaction. & dB triggers are fired

The entire transaction is either commited or rolled back at this point.

2) this would be the exact call sequence that hibernate would make -- it doesn't send extra "insert" statements upon commit. Are you using some sort of auto-commit feature? this might explain why the commit happens on your "insert" statement and fires the first trigger; yet i'd still be puzzled by what you are saying with the "extra" insert happening when you get to your commit.

Again this is my experience with Oracle & usually I make sure autocommit is OFF (the default) at the via hibernate: hibernate.connection.autocommit.

3) What database are you using? and post some code and log files so we can see what is up?

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 9:00 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
On the trigger thing - I was curious and wanted to satisfy it - so I setup a simple example - and it happens that via hibernate my trigger is not fired upon the save and/or the flush but the trigger fired only on commit & That is why I said they fired on commit.

Yet when I went to SQLplus, I noticed the same trigger fired upon insert.

Anyhow sorry if this was misleading. But I don't think it affects figuring out what you are experiencing -- post some code and tell me what database, and I'll see if I can help.

-JT


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 9:13 pm 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
Thanks for the help, its muchly appreciated. Again all I think I need to do is somehow remove the object from the session if an exception is thrown when I call flush (in the DAO code not at the commit code)

I am using Informix 9.2
Autocommit is off

the code
Code:
Transaction transaction;
try {
   transaction = DAOFactory.getCurrentSession().beginTransaction();
   // this calls insert for me
   // the trigger will fire here and will raise an exception
   occupationalHistoryDAO.insert(occupationalHistory);
} catch(Exception e) {
   e.printStackTrace();
}

try {
   // i still want to commit because this is part of a batch process and if a record
   // fails because of the trigger it is logged and needs to be handled manually

   // the trigger is activated here as well.
   // I dont want this to happen
   transaction.commit();
} catch(Exception e) {
   e.printStackTrace();
} finally {
   if(transaction.isActive()) {
       transaction.rollback();
   }
}


the log
Environment 10:59:11,585 INFO Environment:479 - Hibernate 3.1.2

Environment 10:59:11,632 INFO Environment:509 - hibernate.properties not found

Environment 10:59:11,632 INFO Environment:525 - using CGLIB reflection optimizer

Environment 10:59:11,632 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling

Configuration 10:59:11,804 INFO Configuration:1308 - configuring from resource: /hibernate.cfg.xml

Configuration 10:59:11,804 INFO Configuration:1285 - Configuration resource: /hibernate.cfg.xml

Configuration 10:59:13,929 INFO Configuration:1419 - Configured SessionFactory: null

DriverManagerConnectionProvider 10:59:14,335 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)

DriverManagerConnectionProvider 10:59:14,335 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20

DriverManagerConnectionProvider 10:59:14,335 INFO DriverManagerConnectionProvider:45 - autocommit mode: false

SettingsFactory 10:59:14,976 INFO SettingsFactory:77 - RDBMS: Informix Dynamic Server, version: 9.40.UC3

SettingsFactory 10:59:14,991 INFO SettingsFactory:78 - JDBC driver: IBM Informix JDBC Driver for IBM Informix Dynamic Server, version: 2.21.JC5

Dialect 10:59:15,023 INFO Dialect:103 - Using dialect: org.hibernate.dialect.InformixDialect

TransactionFactoryFactory 10:59:15,038 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)

TransactionManagerLookupFactory 10:59:15,070 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)

SettingsFactory 10:59:15,070 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): enabled

SettingsFactory 10:59:15,070 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled

SettingsFactory 10:59:15,085 INFO SettingsFactory:144 - Scrollable result sets: enabled

SettingsFactory 10:59:15,085 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled

SettingsFactory 10:59:15,085 INFO SettingsFactory:160 - Connection release mode: auto

SettingsFactory 10:59:15,085 INFO SettingsFactory:187 - Default batch fetch size: 1

SettingsFactory 10:59:15,085 INFO SettingsFactory:191 - Generate SQL with comments: disabled

SettingsFactory 10:59:15,085 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled

SettingsFactory 10:59:15,085 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory

ASTQueryTranslatorFactory 10:59:15,101 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory

SettingsFactory 10:59:15,101 INFO SettingsFactory:203 - Query language substitutions: {}

SettingsFactory 10:59:15,101 INFO SettingsFactory:209 - Second-level cache: enabled

SettingsFactory 10:59:15,101 INFO SettingsFactory:213 - Query cache: disabled

SettingsFactory 10:59:15,101 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider

SettingsFactory 10:59:15,116 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled

SettingsFactory 10:59:15,116 INFO SettingsFactory:237 - Structured second-level cache entries: disabled

SettingsFactory 10:59:15,116 INFO SettingsFactory:257 - Echoing all SQL to stdout

SettingsFactory 10:59:15,132 INFO SettingsFactory:264 - Statistics: disabled

SettingsFactory 10:59:15,132 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled

SettingsFactory 10:59:15,132 INFO SettingsFactory:283 - Default entity-mode: pojo

SessionFactoryImpl 10:59:15,226 INFO SessionFactoryImpl:153 - building session factory

SessionFactoryObjectFactory 10:59:16,023 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured

Hibernate: select this_.point_type_id as point1_3_0_, this_.point_type_name as point2_3_0_ from sp_point_type this_

Hibernate: select this_.mark_condition_id as mark1_5_0_, this_.mark_condition_name as mark2_5_0_ from sp_mark_condition_type this_

Hibernate: insert into sp_occupational_history (survey_date, local_name, mark_condition_id, mark_pid, point_pid, survey_number) values (?, ?, ?, ?, ?, ?)

JDBCExceptionReporter 10:59:18,679 WARN JDBCExceptionReporter:71 - SQL Error: -525, SQLState: 23000

JDBCExceptionReporter 10:59:18,679 ERROR JDBCExceptionReporter:72 - Failure to satisfy referential constraint Survey Mark PID does not exist.

AbstractFlushingEventListener 10:59:18,695 ERROR AbstractFlushingEventListener:299 - Could not synchronize database state with session

org.hibernate.exception.ConstraintViolationException: could not insert: [nrm.spam.dao.occhist.OccupationalHistoryTO]

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2078)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)

at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)

at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)

at nrm.spam.dao.occhist.hibernate.OccupationalHistoryDAOHibernateImpl.insert(OccupationalHistoryDAOHibernateImpl.java:39)

at nrm.spam.dao.occhist.test.OccupationalHistoryDAOTest.testInsertWithInvalidPointPID(OccupationalHistoryDAOTest.java:121)

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 junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.main(Unknown Source)

Caused by: java.sql.SQLException: Failure to satisfy referential constraint Survey Mark PID does not exist.

at com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3082)

at com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3396)

at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2259)

at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2179)

at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:721)

at com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:305)

at com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:882)

at com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedStatement.java:281)

at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)

... 25 more

nrm.common.dao.exceptions.DAOException: org.hibernate.exception.ConstraintViolationException: could not insert: [nrm.spam.dao.occhist.OccupationalHistoryTO]

at nrm.spam.dao.occhist.hibernate.OccupationalHistoryDAOHibernateImpl.insert(OccupationalHistoryDAOHibernateImpl.java:43)

at nrm.spam.dao.occhist.test.OccupationalHistoryDAOTest.testInsertWithInvalidPointPID(OccupationalHistoryDAOTest.java:121)

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 junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.main(Unknown Source)

Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [nrm.spam.dao.occhist.OccupationalHistoryTO]

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2078)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)

at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)

at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)

at nrm.spam.dao.occhist.hibernate.OccupationalHistoryDAOHibernateImpl.insert(OccupationalHistoryDAOHibernateImpl.java:39)

... 16 more

Caused by: java.sql.SQLException: Failure to satisfy referential constraint Survey Mark PID does not exist.

at com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3082)

at com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3396)

at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2259)

at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2179)

at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:721)

at com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:305)

at com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:882)

at com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedStatement.java:281)

at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)

... 25 more

Hibernate: insert into sp_occupational_history (survey_date, local_name, mark_condition_id, mark_pid, point_pid, survey_number) values (?, ?, ?, ?, ?, ?)

JDBCExceptionReporter 10:59:24,945 WARN JDBCExceptionReporter:71 - SQL Error: -525, SQLState: 23000

JDBCExceptionReporter 10:59:24,945 ERROR JDBCExceptionReporter:72 - Failure to satisfy referential constraint Survey Mark PID does not exist.

AbstractFlushingEventListener 10:59:24,945 ERROR AbstractFlushingEventListener:299 - Could not synchronize database state with session

org.hibernate.exception.ConstraintViolationException: could not insert: [nrm.spam.dao.occhist.OccupationalHistoryTO]

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2078)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)

at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)

at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)

at nrm.spam.test.AbstractSPAMTestCase.tearDown(AbstractSPAMTestCase.java:245)

at nrm.spam.dao.occhist.test.OccupationalHistoryDAOTest.tearDown(OccupationalHistoryDAOTest.java:167)

at junit.framework.TestCase.runBare(TestCase.java:130)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.main(Unknown Source)

Caused by: java.sql.SQLException: Failure to satisfy referential constraint Survey Mark PID does not exist.

at com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3082)

at com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3396)

at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2259)

at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2179)

at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:721)

at com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:305)

at com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:882)

at com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedStatement.java:281)

at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)

... 22 more

org.hibernate.exception.ConstraintViolationException: could not insert: [nrm.spam.dao.occhist.OccupationalHistoryTO]

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2078)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)

at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)

at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)

at nrm.spam.test.AbstractSPAMTestCase.tearDown(AbstractSPAMTestCase.java:245)

at nrm.spam.dao.occhist.test.OccupationalHistoryDAOTest.tearDown(OccupationalHistoryDAOTest.java:167)

at junit.framework.TestCase.runBare(TestCase.java:130)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(Unknown Source)

at com.borland.jbuilder.unittest.JBTestRunner.main(Unknown Source)

Caused by: java.sql.SQLException: Failure to satisfy referential constraint Survey Mark PID does not exist.

at com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3082)

at com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3396)

at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2259)

at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2179)

at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:721)

at com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:305)

at com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:882)

at com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedStatement.java:281)

at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)

at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)

... 22 more

JDBCExceptionReporter 10:59:24,976 WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: 01I01

JDBCExceptionReporter 10:59:24,976 WARN JDBCExceptionReporter:49 - Database has transactions

JDBCExceptionReporter 10:59:24,976 WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: 01I04

JDBCExceptionReporter 10:59:24,976 WARN JDBCExceptionReporter:49 - Database selected


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 10:00 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
good news and bad news:

1) good - I was able to reproduce your problem in my env.
I now see exactly what you are talking about.

2) bad - I don't have a solution... but will see if I can find one.

-JT

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 10:02 pm 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
Thanks jt_1000, thats is muchly appreciated.

I tried calling evict straight after the flush but that had no effect.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 10:56 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
check this out:

on the first page, last paragraph of the API docs for Session it states:

Code:
If the Session throws an exception, the transaction MUST be rolled back and the session discarded.  The internal state of the Session might not be consistent with the database after the exception occurs.


I think you are out of luck.

The only work-around I can possibly think of would be to start a new session and merge your good objects into that new session - yet that sounds like a pain-in-the-???.

I did this as the work around and it worked, but again a pain if you have a lot of objects.


Code:

tx=sess.beginTransaction();

Person p1=new Person();
p1.setFIRST_NAME("JOHN");
p1.setLAST_NAME("THOMPSON");
sess.saveOrUpdate(p1);   

Person bad=new Person();
bad.setFIRST_NAME("JJ");    //my trigger on person will kick back JJ's
bad.setLAST_NAME("THOMPSON");
sess.saveOrUpdate(bad); 

try
{
sess.flush();    //this will fail due to SQL trigger application error.
       //not allowing any JJ first names.
}
catch (Exception e)
{
   System.err.println("Exception thrown:" + e.getMessage());
   e.printStackTrace();
   try
   {
       tx.rollback();
       sess=ms.getSessionNew();
       tx=sess.beginTransaction();
       sess.merge(p1);
       hbm_instance=null;
   }
   catch (Exception e1)
   {
      System.out.println("rollback failed!");
   }
}
Person p2=new Person();
p2.setFIRST_NAME("JOHN");
p2.setLAST_NAME("THOMPSON");
sess.saveOrUpdate(p2);   //id is assigned at this point, prior to commit.
sess.flush();
tx.commit();

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 12:04 am 
Newbie

Joined: Sun Feb 05, 2006 11:46 pm
Posts: 13
Thanks, I think that just about sums it up in the API. I'll have to look into how I can change my process.


Top
 Profile  
 
 Post subject: Re: Problem with flush and commit
PostPosted: Mon Sep 28, 2015 5:04 am 
Newbie

Joined: Mon Sep 28, 2015 4:56 am
Posts: 1
Hi ,

I am facing the same problem.

Hibernate flush method is throwing constraint

Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.CacheSynchronization.beforeCompletion(CacheSynchronization.java:88)
at org.hibernate.transaction.WebSphereExtendedJTATransactionLookup$TransactionManagerAdapter$TransactionAdapter$1.invoke(WebSphereExtendedJTATransactionLookup.java:209)
at $Proxy137.beforeCompletion(Unknown Source)
at com.ibm.ws.jtaextensions.SynchronizationCallbackWrapper.beforeCompletion(SynchronizationCallbackWrapper.java:66)
at com.ibm.tx.jta.impl.RegisteredSyncs.coreDistributeBefore(RegisteredSyncs.java:291)
at com.ibm.ws.tx.jta.RegisteredSyncs.distributeBefore(RegisteredSyncs.java:152)
at com.ibm.ws.tx.jta.TransactionImpl.prePrepare(TransactionImpl.java:2338)
at com.ibm.ws.tx.jta.TransactionImpl.stage1CommitProcessing(TransactionImpl.java:557)
at com.ibm.tx.jta.impl.TransactionImpl.processCommit(TransactionImpl.java:1015)
... 34 more
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint



We have handled in code that if data already present should not go and create the new record in DB.
This problem is when there is large number of records to process in one transaction.

Any pointers please,Thanks in advance.


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