On May 9, 2008, at 6:15 AM, Gegas wrote:



David Blevins wrote:

Heh :)  It's not so stupid as we did put some fancy code for mapping
the datasources to your persistence.xml without which you might
actually need to have separate persistence.xmls.  But we work hard to
adapt things to the environment you setup so these kinds of issues are
avoided.  And then of course we went the extra mile to make sure you
could setup your entire environment right in your test case, config
file free, which is nice icing on the cake.  That also allows you to
shut off things like transaction or security processing, which can be
nice.  Well, technically you're turning *on* the mock transaction
manager and/or security service we provide, but you get the idea :)


You did a very good job :-)

Thanks!  Really happy you like it.  Tell all your friends ;)

David Blevins wrote:

The one thing that you really might want a different persistence.xml
for is maybe there is some JPA provider specific property you use in
production that you don't want enabled in test (or vice versa).  I
suspect that'll be a rare case, but it's certainly something to keep
an eye out for.


I've found something that's a real case. The unit tests uses OpenEJB and my production environment is JBoss. The property for TransactionManager is
different for test and production:

OpenEJB

hibernate.transaction.manager_lookup_class  =
org.apache.openejb.hibernate.TransactionManagerLookup


JBoss

hibernate.transaction.manager_lookup_class =
org.hibernate.transaction.JBossTransactionManagerLookup


Now I really would need the ability to use a seperate persistence.xml for
testing.

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();
    }
}

Reply via email to