-
Jan 21st, 2013, 05:04 AM
#1
Spring3 - Mybatis3 programmatically transaction with PlatformTransactionManager
Hi,
i have a problem with programmatically transaction management in String.
My persistence layer is Mybatis.
My Mappers are automatically generated by MaBatis Generator. This is an Eclipse plugin.
I have 2 datasource and 2 transaction manager.
I can't use JTA for transaction and i use DataSourceTransactionManager.
I have a class with a method that manage the operations on database.
In this method i would like to programmatically manage the transaction.
This is the snippet of my code:
@Autowired
@Qualifier("txManager")
PlatformTransactionManager txManager;
@Autowired
@Qualifier("txManagerUtente")
PlatformTransactionManager txManagerUtente;
@Override
public void elaborateRequest(String body, String cookie) throws Exception
{
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.P ROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
TransactionStatus statusUtente = txManagerUtente.getTransaction(def);
try{
ServiceInterface serviceImpl = (ServiceInterface) this;
serviceImpl.elaborateRequestImpl(body);
}catch(Exception e)
{
txManager.rollback(status);
txManagerUtente.rollback(statusUtente);
Logger.getLogger(getClass().getName()).error("Ecce zione nell'elaborazione della request...Rolling back db backoffice", e);
Logger.getLogger(getClass().getName()).error("Ecce zione nell'elaborazione della request...Rolling back db utente", e);
throw e;
}
txManager.commit(status);
txManagerUtente.commit(statusUtente);
}
This is my xml configuration for persistence layer:
<bean id="dataSourceConnection"
class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName" value="${controllore.jdbc.driver}" />
<property name="url" value="${controllore.jdbc.url}" />
<property name="username" value="${controllore.jdbc.username}" />
<property name="password" value="${controllore.jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceConnection" />
<property name="configLocation"
value="classpath:......../controllore/mybatis/xml/MapperConfig.xml"></property>
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSourceConnection" />
</bean>
<!-- tx:annotation-driven transaction-manager="txManager" /-->
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConf igurer">
<property name="basePackage" value="..........controllore.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="dataSourceConnectionUtente"
class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName" value="${controllore.jdbc.dbutente.driver}" />
<property name="url" value="${controllore.jdbc.dbutente.url}" />
<property name="username" value="${controllore.jdbc.dbutente.username}" />
<property name="password" value="${controllore.jdbc.dbutente.password}"/>
</bean>
<bean id="sqlSessionFactoryUtente" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceConnectionUtente" />
<property name="configLocation"
value="classpath:......./mybatis/xml/MapperConfigDbUtente.xml"></property>
</bean>
<bean id="txManagerUtente"
class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSourceConnectionUtente" />
</bean>
<!--tx:annotation-driven transaction-manager="txManagerUtente" /-->
<bean class="org.mybatis.spring.mapper.MapperScannerConf igurer">
<property name="basePackage" value=".........controllore.dbutente.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactoryUtente" />
</bean>
My problem is that when i execute the code
txManager.commit(status);
txManagerUtente.commit(statusUtente);
i have the following exeption:
org.springframework.transaction.IllegalTransaction StateException: Transaction is already completed - do not call commit or rollback more than once per transaction
but when i have an exeption the rollback work fine.
Why i have an autocommit and my commit throw an exeption???
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules