smallufo smallufo - 1 year ago 69
Java Question

When transactionManager is not named "transactionManager"

I am trying Spring 3(.0.2.RELEASE) and JPA2 and Hibernate 3.5.1-Final...
One thing upsets me is that spring seems only accept a transaction Manager named "transactionManager"

If I don't name it "transactionManager" , Spring will throws NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined.

Here is my config :

<context:component-scan base-package=""/>


<bean id="miningEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="mining"/>

<bean id="miningTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="entityManagerFactory" ref="miningEntityManagerFactory"/>

<tx:advice id="txAdviceMining" transaction-manager="miningTransactionManager">
<tx:method name="get*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>

<aop:pointcut id="methods" expression="execution(* destiny.utils.AbstractDao+.*(..))"/>
<aop:advisor advice-ref="txAdviceMining" pointcut-ref="methods"/>

<tx:annotation-driven transaction-manager="miningTransactionManager"/>

In this config , an Entity Manager Factory is not necessarily named "entityManagerFactory" , and "txAdvice" is not necessarily named "txAdvice" , either.
But I don't know why on earth Spring requires a transaction manager named "transactionManager" ?

Is there any way not to name a transaction manager "transactionManager" ?
(I'm running multiple spring config files , so I try my best to avoid name-conflicting)

test code :

public class MiningPersonDaoTest
private EntityManagerFactory miningEntityManagerFactory;

private MiningPersonDao miningPersonDao;

public void testUpdate()
MiningPerson p = miningPersonDao.get(42L);

Answer Source

My understanding is that in the context of unit tests (TransactionalTestExecutionListener), the code that otherwise looks up the transaction manager is not used (TransactionInterceptor#determineTransactionManager).

You could try to annotate your test class with @TransactionConfiguration, which accepts a transactionManager attribute. Not the most elegant way, but possibly the best option for the time being.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download