Monday, May 30, 2011

Spring PersistenceUnitPostProcessor

I needed 2 persistence.xml files, one for testing and one for production.
The only difference was the transaction-type which I had to set to JTA, as we were using Atomikos as a transaction manager for production. But for unit testing, resource-local was a better choice (less configuration).

The solution I found was to use a "PersistenceUnitPostProcessor" which changes the value after loading.




















The interesting part is the persistenceUnitPostProcessors property which registers a post processor.
Notice also the persistenceXmlLocation property. As we deploy on Websphere and make use of JPA 2.0, we can't name our file persistence.xml. It would conflict with Webphere !

The "TransactionTypeSelectorPersistenceUnitPostProcessor" java class is trivial, it just sets the transaction type.


public class TransactionTypeSelectorPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {

private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;

@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
pui.setTransactionType(transactionType);
}

public void setTransactionType(PersistenceUnitTransactionType transactionType) {
this.transactionType = transactionType;
}

public PersistenceUnitTransactionType getTransactionType() {
return transactionType;
}

}

Wednesday, May 4, 2011

Setting tomcats'language

I unfortunately have to work with computers with default language set to German.
Until now I was always able to change the language, but Windows does really support this. Which means I have part of my computer in English and many application remain in German.
It is very annoying for stack trace for instance.

Thus you can set the language of apache tomcat via a system property.
In eclipse you can set

-Duser.language=en

as VM argument to the launch configuration of tomcat.