tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <david.blev...@visi.com>
Subject Re: No Maven2 Best Practices?
Date Tue, 13 May 2008 18:09:53 GMT

On May 13, 2008, at 4:38 AM, Gegas wrote:

>
>
> David Blevins wrote:
>>
>>
>> Don't know why I didn't think of this before, but we could just add
>> some support for checking and resetting that property if hibernate is
>> your persistence provider.  Not sure why they make you set it in the
>> first place, they could easily figure out which lookup strategy to  
>> use
>> on their own.
>>
>> In the meantime, you can use a custom lookup strategy like this:
>>
>> import org.hibernate.HibernateException;
>> import org.hibernate.transaction.TransactionManagerLookup;
>> import javax.transaction.TransactionManager;
>> import java.util.Properties;
>>
>> public class DynamicTransactionManagerLookup implements
>> TransactionManagerLookup {
>>
>>     private TransactionManagerLookup impl;
>>
>>     public DynamicTransactionManagerLookup() {
>>         String[] strategies = {
>>
>> "org.apache.openejb.hibernate.TransactionManagerLookup",
>>
>> "org.hibernate.transaction.JBossTransactionManagerLookup"
>>         };
>>         for (String className : strategies) {
>>             try {
>>                 Class<?> clazz =
>> this.getClass().getClassLoader().loadClass(className);
>>                 impl = (TransactionManagerLookup)  
>> clazz.newInstance();
>>             } catch (Exception e) {
>>             }
>>         }
>>
>>         if (impl == null) throw new IllegalStateException("No
>> TransactionManagerLookup available");
>>     }
>>
>>     public TransactionManager getTransactionManager(Properties
>> properties) throws HibernateException {
>>         return impl.getTransactionManager(properties);
>>     }
>>
>>     public String getUserTransactionName() {
>>         return impl.getUserTransactionName();
>>     }
>> }
>>
>>
>>
>
> I'm sorry David but I cannot see how this would help? The code isn't
> complete and that's intended, or am I missing something?

That's what I get for not testing it first :)  Forgot a break  
statement.  Updated code at the bottom of this page:
http://cwiki.apache.org/OPENEJBx30/hibernate.html

The technique is that the code first tries to find the OpenEJB  
TransactionManagerLookup strategy and if it can't find that it goes to  
the JBoss one.  So in your testing where OpenEJB is present that's the  
one that will get used.  In production where OpenEJB is not present,  
the JBoss TransactionManagerLookup will get used.

You could use some other technique to detect the environment you're  
in, but the basic idea is that the custom TransactionManagerLookup  
strategy is what gets used in your persistence.xml so you can support  
both platforms with the same xml file.

-David


Mime
View raw message