Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / July 2006

Tip: Looking for answers? Try searching our database.

bullshit JBoss JTA transactions with Hibernate persistance

Thread view: 
timasmith@hotmail.com - 06 Jul 2006 19:26 GMT
I think I am fairly smart guy, but the transaction management in
Hibernate with JBoss is ****** obscure.

On a side note I am sick, sick, sick to death of smart a.s authors and
online article gurus writing a few paragraphs on how to do xyz with
'snippets'.  Read my lips:

POST A WORKING EXAMPLE (or get out of my google search results)

I have spent the entire day reading 'Hibernate in action' (or not in my
case), online documentation, blogs, blurbs, forums and far fetched
figures on how to configure transaction management with JBoss.

Yes I had a single 'insert' working, calling it multiple times with
many readonly reads before and after.

No, I have never had the plain jane scenario work of:

a) start a session /transaction/ transaction on a session/ using JTA or
not or JNDI or not

b) save (insert) a model

c) generate a failure

d) watch it all rollback

No, one transaction - not a problem.  Multiple either my transaction
has not successfully started, my session closed itself, the transaction
was nested, some hidden AOP flip flop intercepted and crapped over my
code,

I could post code, but its pointless - I could never do justice on all
the combinations I have tried...

I feel JDBC generated code coming back into my vogue...

...unless someone can save me.
John Liptak - 07 Jul 2006 21:42 GMT
>I think I am fairly smart guy, but the transaction management in
>Hibernate with JBoss is ****** obscure.
[quoted text clipped - 4 lines]
>
>POST A WORKING EXAMPLE (or get out of my google search results)

 Well, some of us work at companies that would not allow a complete
working example.  I think that snippits are better than nothing.

>I have spent the entire day reading 'Hibernate in action' (or not in my
>case), online documentation, blogs, blurbs, forums and far fetched
>figures on how to configure transaction management with JBoss.

 hibernate in action does not cover JBoss specific JTA transactions
because it was based mostly on the 2.x code.  The next edition due
in November should do better.

>Yes I had a single 'insert' working, calling it multiple times with
>many readonly reads before and after.
[quoted text clipped - 14 lines]
>was nested, some hidden AOP flip flop intercepted and crapped over my
>code,

 You can only have ONE JTA transaction active at a time.  There is no
nesting.

>I could post code, but its pointless - I could never do justice on all
>the combinations I have tried...
>
>I feel JDBC generated code coming back into my vogue...
>
>...unless someone can save me.

 We could, but you didn't post what you are doing :-)
Necessary items:

1.) How is your JTA transaction being created?  
    EJB required, web service call, manually?

2.) How is your JTA transaction being committed?  
   container, servlet filter, manually?

3.) How are you generating your failure?  
    setRollbackOnly()?  throw exception?

based on that information and the problem you are having, we may be
able to help.
timasmith@hotmail.com - 08 Jul 2006 01:38 GMT
In the end I got it working with HSql but once I switched to PostgreSQL
I could only get the JDBC transaction factory working.  Possibly due to
some obscure service errors JBoss now kicks out on startup.

Judging from other replies there is *of course* a better way to do
this.  That is one of my frustrations - every time I make some progress
I find another article that indicates there is a better way.

There were some excellent sections in the Hibernate in action book, I
especially liked the architecture tips.  Now it is dated as I am using
EJB 3.0 with JBoss 4.0.4 (which incidently appears to ******* rock.
Coupled with Hibernate and all the other technologies you can do some
serious enterprise apps in a small about of time.

Anyway here is my suboptimal postgresql transaction setup

JBoss 4.0.4
EJB 3.0 Java 1.5.0.7
Hibernate3.jar that shipped with it 5/15/2006

public static void store(IBaseModel model, boolean continueSession)
throws Exception {
try {
if (model.getId() == null) {
HibernateUtil.getSession().save(model);
} else {
IBaseModel original = (IBaseModel)
load(model.getClass(),model.getId(),true);
model.copyModifiedTo(original);
HibernateUtil.getSession().save(original);
}
if (!continueSession) {
HibernateUtil.endSession();
}
} catch (Exception e) {
Log.warn(model);
HibernateUtil.rollbackSession();
throw e;
}
}

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.myproject.framework.logging.Log;

public class HibernateUtil {

private static Session currentSession = null;
private static SessionFactory sessionFactory = null;
private static Transaction transaction = null;

/**
* Configure the sessionFactory from hibernate.cfg.xml
*/
static {
try {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace(System.out);
ex.printStackTrace(System.err);
Log.fatal(ex);
System.exit(1);
}
}

/**
* Return the sessionFactory from cache
*
* @return
* @throws Exception
*/
private static SessionFactory getSessionFactory() throws Exception {
if (sessionFactory == null) {
throw new NullPointerException();
} else {
return sessionFactory;
}
}

/**
* Return the current session if it exists otherwise
* open a new session and start a new transaction
*
* @return
* @throws Exception
*/
public static Session getSession() throws Exception {
if (currentSession == null) {
currentSession = getSessionFactory().openSession();
if (currentSession == null) {
throw new NullPointerException();
}
transaction = currentSession.beginTransaction();
}
return currentSession;
}

/**
* Commit the transaction, rollback on error and close the session
*
* @throws Exception
*/
public static void endSession() throws Exception {
Session session = null;
try {
session = getSession();
transaction.commit();
} catch (Exception ex) {
Log.error(ex);
transaction.rollback();
} finally {
try {
session.close();
} catch (Exception ex2) {
Log.error(ex2);
} finally {
cleanup();
}
}
}

/**
* Remove the session and transaction from cache
*/
private static void cleanup() {
transaction = null;
currentSession = null;
}

/**
* Rollback the transaction and close the session
*
* @throws Exception
*/
public static void rollbackSession() {
try {
if (transaction != null) {
transaction.rollback();
}
} finally {
try {
Session session = getSession();
if (session != null) {
session.close();
}
} catch (Exception ex2) {
Log.error(ex2);
} finally {
cleanup();
}
}
}

}

package com.myproject.model.common;

public interface IBaseModel {

/**
* Unique database identifier
*
* @return
*/
public Long getId();

/**
* Copy all modified fields to the target model
*
* @param model
*/
public void copyModifiedTo(IBaseModel model);

}

hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="current_session_context_class">thread</property>
<property
name="hibernate.connection.driver_class">org.postgresql.Driver</property>

<property
name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>

<property name="hibernate.connection.username">myproject</property>
<property name="hibernate.connection.password">myproject</property>
<property
name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property
name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<property name="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property
name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>

<property name="jta.UserTransaction">UserTransaction</property>

<mapping
resource="com/myproject/model/patient/PatientModel.hbm.xml"/><mapping
resource="com/myproject/model/common/AddressModel.hbm.xml" />

</session-factory>

</hibernate-configuration>

> >I think I am fairly smart guy, but the transaction management in
> >Hibernate with JBoss is ****** obscure.
[quoted text clipped - 59 lines]
> based on that information and the problem you are having, we may be
> able to help.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.