I just upgraded from Hibernate 3.5 to 4.1.2. I'm using Atomikos 3.7 as the JTA provider. I implemented JtaPlatform by extending AbstractJtaPlatform(which was not obvious from the docs nor migration guide that I needed to do this). Everything seems to be mostly working OK except for my Intercepter implementation that isn't having 'afterTransactionCompletion' invoked on it. Not having it invoked is leaving some thread local data that is normally cleaned up in 'afterTransactionCompletion' around. But beforeTransactionCompletion, onSave, etc are being invoked. I debugged a little bit and I see the problem/inconsistency. The other methods don't check if 'TransactionImplementor hibernateTransaction' is null, and just invoke the interceptor. What is bothering me is that 3.5.x essentially does the same thing(the tx != null check for the after invocation), but that release worked fine for me. For some reason the transaction synchronizations appear to be working but there isn't an associated Hibernate transaction to go with the JTA transaction. Other than that data gets persisted just fine, but not being able to clean up my interceptors state post transactions is a problem.
Here is what my 3.5.x transaction manager lookup and factory combo essentially looked like:
Code:
public class AtomikosTransactionManagerLookup implements org.hibernate.transaction.TransactionManagerLookup {
private UserTransactionManager utm;
public AtomikosTransactionManagerLookup() {
utm = new UserTransactionManager();
}
@Override
public TransactionManager getTransactionManager(Properties props) throws HibernateException {
return utm;
}
@Override
public String getUserTransactionName() {
return null;
}
@Override
public Object getTransactionIdentifier(Transaction transaction) {
return transaction;
}
}
public class AtomikosJTATransactionFactory extends JTATransactionFactory {
private UserTransaction userTransaction;
@Override
public void configure ( Properties props ) throws HibernateException {
try {
super.configure ( props );
} catch ( Exception e ) {
String msg = "Hibernate: error during config - ignore for hibernate 3.2.7 or higher";
Configuration.logDebug ( msg , e );
}
}
@Override
protected UserTransaction getUserTransaction() {
if (this.userTransaction == null) {
this.userTransaction = new UserTransactionImp();
}
return this.userTransaction;
}
}
And here is my 4.x Atomikos JTA platform. I just plugin the field values from externally configured userTransaction and manager(yes I'm in major hack mode right now).
Code:
public class AtomikosJtaPlatform extends AbstractJtaPlatform {
public static UserTransaction userTransaction;
public static UserTransactionManager userTransactionManager;
@Override
protected TransactionManager locateTransactionManager() {
return userTransactionManager;
}
@Override
protected UserTransaction locateUserTransaction() {
return userTransaction;
}
}
I'm configuring using the javax.persistence.Persist API since Ejb3Configuration is now deprecated(what I was using in 3.5.x):
Code:
overrides.put("hibernate.ejb.interceptor", MyInterceptor.class.getName());
overrides.put( AvailableSettings.JTA_PLATFORM, AtomikosJtaPlatform.class.getName() );
overrides.put( AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "jta");
overrides.put( AvailableSettings.RELEASE_CONNECTIONS, "after_statement");
overrides.put("hibernate.connection.datasource", datasourceName);
//tried only setting jtaDataSource, but that seemed to be problematic
overrides.put("javax.persistence.jtaDataSource", datasourceName);
… lots of other unrelated settings
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu", overrides);
I don't know if its correct that there isn't a hibernate transaction('TransactionImplementor hibernateTransaction') or not in 4.x when I'm using JTA. If it isn't correct I'm wondering what I'm doing wrong that is preventing the associated Hibernate Transaction from being created to be paired with the JTATransaction.
Anything obvious here? Would be great if a core developer can chime in on whether or not something is obviously wrong here or if someone whom has gotten Atomikos to work with 4.x could chime in with an example.
Thanks,
ZK