We are using Hibernate 2 with JBoss 4.2.0 and Struts 1.2.7.
We are not using any EJB !
I have the persisted object 'dbCategory' of class AmpCategoryClass which has a List called "possibleValues" of AmpCategoryValue objects. (possibleValues has the 'all-delete-orphans' property set.)
Now, whithin the transaction I am deleting several objects from the List 'possibleValues' but not all of them can actually be deleted from the database since they are referred by other objects (FOREIGN KEY).
So tx.commit() throws an Exception. And in the catch I try to rollback. But nothing happens. I even checked in the MySQL query log....and there is no query generated by the rollback function.
I'd be grateful for any help I can get..cause right now the database remains in an inconsistent state :( .
Thanks
Hibernate version: 2.1.7
Code between sessionFactory.openSession() and session.close():
dbSession = PersistenceManager.getSession(); // does sf.openSession();
tx = dbSession.beginTransaction();
....
for (int i=0; i<possibleValues.length; i++) {
if ( k < dbCategory.getPossibleValues().size() && !addToPossibleValues ) {
AmpCategoryValue ampCategoryValue = (AmpCategoryValue)dbCategory.getPossibleValues().get(k);
if (possibleValues[i].equals("")) {// In this block we are surely editing an existing category (not creating a new one)
AmpCategoryValue removedValue = (AmpCategoryValue)dbCategory.getPossibleValues().remove( k );
....
tx.commit();
}
} catch (Exception ex) {
if (tx != null)
tx.rollback();
retValue = false;
} finally {
try {
PersistenceManager.releaseSession(dbSession); // does dbSession.close();
} catch (Exception ex2) {
logger.error("releaseSession() failed :" + ex2);
}
Full stack trace of any exception that occurs:
16:03:45,219 WARN [JDBCExceptionReporter] SQL Error: 1451, SQLState: 23000
16:03:45,219 ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`amp_staging/amp_activities
_categoryvalues`, CONSTRAINT `FKDF2127572F19A6C2` FOREIGN KEY (`amp_categoryvalue_id`) REFERENCES `amp_category_value` (`id`))
16:03:45,221 WARN [JDBCExceptionReporter] SQL Error: 1451, SQLState: 23000
16:03:45,221 ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`amp_staging/amp_activities
_categoryvalues`, CONSTRAINT `FKDF2127572F19A6C2` FOREIGN KEY (`amp_categoryvalue_id`) REFERENCES `amp_category_value` (`id`))
16:03:45,221 ERROR [SessionImpl] Could not synchronize database state with session
Name and version of the database you are using: MySQL 5.0.45
The generated SQL (show_sql=true):
080506 16:02:37 41 Query select ampcategor0_.id as id, ampcategor0_.category_name as category2_, ampcategor0_.keyName as keyName, ampcategor0_.description as descript4_, ampcategor0_.is_multiselect as is_multi5_, ampcategor0_.is_ordered as is_ordered from AMP_CATEGORY_CLASS ampcategor0_ where (ampcategor0_.id=1 )
41 Query select possibleva0_.amp_category_class_id as amp_cate3___, possibleva0_.id as id__, possibleva0_.index_column as index_co4___, possibleva0_.id as id0_, possibleva0_.category_value as category2_0_, possibleva0_.amp_category_class_id as amp_cate3_0_, possibleva0_.index_column as index_co4_0_ from AMP_CATEGORY_VALUE possibleva0_ where possibleva0_.amp_category_class_id=1
080506 16:03:45 41 Query update AMP_CATEGORY_VALUE set category_value='B', amp_category_class_id=1, index_column=4 where id=228
41 Query update AMP_CATEGORY_VALUE set category_value='C', amp_category_class_id=1, index_column=5 where id=229
41 Query delete from AMP_CATEGORY_VALUE where id=5
41 Query delete from AMP_CATEGORY_VALUE where id=6
41 Query delete from AMP_CATEGORY_VALUE where id=227
41 Query SHOW FULL TABLES FROM `amp_staging` LIKE 'PROBABLYNOT'
Hibernate properties
## MySQL
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
#transaction manager
hibernate.transaction.factory_class = net.sf.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = net.sf.hibernate.transaction.JBossTransactionManagerLookup
hibernate.max_fetch_depth 2
hibernate.use_outer_join true
hibernate.hbm2ddl.auto update
#DEVELOPMENT ONLY!
hibernate.cglib.use_reflection_optimizer=false
hibernate.connection.autocommit=false
## JNDI settings
hibernate.connection.datasource=java:ampLocalStagingDS
##transaction isolation: 2 = READ-COMMITED, check with your mysql.ini "transaction-isolation" variable
hibernate.connection.isolation=2
#### Cache Settings #####
hibernate.cache.provider_class=net.sf.hibernate.cache.TreeCacheProvider
#hibernate.cache.use_query_cache true
######################
### Query Language ###
######################
## define query language constants / function names
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
hibernate.show_sql false
#hibernate.show_sql true
hibernate.jdbc.use_streams_for_binary true
#################################
### Hibernate Connection Pool ###
#################################
hibernate.connection.pool_size 20
hibernate.statement_cache.size 200
###################################
### Stale-tolerant queries impl ###
###################################
#hibernate.cache.query_cache_factory=org.digijava.kernel.persistence.StaleTolerantQueryCacheFactory
# Tables that should be stale-tolerant for queries:
# hibernate.cache.stale_tolerable_query_spaces=EP_NOTICES,RC_ITEMS
|