-->
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.  [ 7 posts ] 
Author Message
 Post subject: Transaction.rollback doesn't work at all.
PostPosted: Tue May 06, 2008 9:06 am 
Newbie

Joined: Mon Apr 26, 2004 5:54 am
Posts: 8
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 4:32 pm 
Newbie

Joined: Mon Apr 26, 2004 5:54 am
Posts: 8
Guys....please...if you have any idea on this.
I know I'm doing something wrong.....but if you could at least give me a hint about what problems to look for...that would be very helpful. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 11:05 am 
Newbie

Joined: Wed May 07, 2008 6:21 pm
Posts: 6
Location: Lakewood, CO
Sharkie,

I have basically the same code and it works OK for me. Are you sure the rollback is executing? Is tx really not null at that time?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 11:20 am 
Newbie

Joined: Mon Apr 26, 2004 5:54 am
Posts: 8
Thanks for the response Daniel.

Yes I am sure that the transaction is not null. I debugged it ... and also have turned on the Query Log in MySQL.
When tx.rollback() function is executed there is no output in the mysql query log....which I think it is really weird.

What I did to solve the problem...was change the hibernate.properties from:
hibernate.transaction.factory_class = net.sf.hibernate.transaction.JTATransactionFactory

to:
hibernate.transaction.factory_class = net.sf.hibernate.transaction.JDBCTransactionFactory

and now the rollback works.

I really don't understand why the JTATransactionFactory didn't work ! Maybe it was because we are using c3p0 ? Or did we miss something in the configuration of JBoss and hibernate ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 11:56 am 
Newbie

Joined: Wed May 07, 2008 6:21 pm
Posts: 6
Location: Lakewood, CO
Sorry Sharkie, I don't know the diff between those classes. I have been using org.hibernate.transaction.JDBCTransactionFactory. But I also have been using any factory_class lately. It might not help but here is the config file I'm using (I've been running with the "server environment" details commented).

<!-- Following Properties to be used in Server environment
<property name="connection.datasource">jndi/qcstagingUniface</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="generate_statistics">false</property>
<property name="connection.release_mode">auto</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
-->

<!-- Following Properties to be used in JUnit environment -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<property name="connection.url">jdbc:oracle:thin:@tssc700:1526:LI</property>
<property name="connection.username">icc</property>
<property name="connection.password">Password$</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 2:22 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Connections not sourced from JBoss' JCA connection pool will not be enlisted in JTA transactions, so using JDBCTransactionFactory is the right way. Though, it doesn't make much sense to use an EE container at all if you're ignoring JTA, EJB and the JBoss connection pool...

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 4:47 am 
Newbie

Joined: Mon Apr 26, 2004 5:54 am
Posts: 8
Thanks cbredesen. You are right. We would like to use JTA transactions.

So, did I get it right that we cannot use JTA transactions with c3p0 ?


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