PDA

View Full Version : New to Spring-Transaction managment problem



skamiset
Sep 14th, 2004, 10:39 AM
Hi,

I just started using Spring in one of the projects. I am having problem with inserts. I have two insert statements. I call my service class in my Action calss like

getEelService.insertInvoice(inv);
getEelService.insertLine(lin);

I have seperate Daos for thse two. But my req is that if any one of them fail becuase of constraint violation otherone should also roll back too. Can some help me with this pls :( . I will appreciate u r help very much. Following is snippets of my code for InsertinvoiceDao

import com.ibatis.dao.client.Dao

public interface InsertinvoiceDao extends Dao {

public void insertInvoice (Object parameterObject);
}

Here is impl for above Interface


import org.springframework.orm.ibatis.support.SqlMapClien tDaoSupport;

public class InsertinvoiceSqlMapDao extends SqlMapClientDaoSupport
implements InsertinvoiceDao{

public void insertInvoice(Object parameterObject) {

getSqlMapClientTemplate().insert("insertInvoice",parameterObject);

}
}


Follwoing is my applicationContext file

<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!-- ========================= GENERAL DEFINITIONS
=========================== -->

<!--
<bean id="loggerInterceptor"
class="com.wfs.customerinfo.services.LoggerInterceptor">
<property name="priority">
<value>INFO</value>
</property>
<property name="logBegin">
<value>false</value>
</property>
<property name="logTime">
<value>true</value>
</property>
</bean>
-->

<bean id="mailSession"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property
name="jndiName"><value>java:comp/env/mail/MailSession</value></property>
</bean>

<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderIm pl">
<property name="session"><ref
bean="mailSession"/></property>
</bean>

<bean id="emailService"
class="com.wfs.eel.services.EmailServiceImpl">
<property name="mailSender"><ref
bean="mailSender"/></property>
</bean>


<!-- ========================= PERSISTENCE DEFINITIONS
======================= -->

<!--
- JNDI DataSource
-->
<bean id="appDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/EelDS</value>
</property>
</bean>

<!--
- Transaction manager for usage in business or data access objects.
-->
<bean id="appTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTran sactionManager">
<property name="dataSource">
<ref local="appDataSource"/>
</property>
</bean>

<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactory Bean">
<property name="configLocation">

<value>classpath:/com/wfs/eel/resources/sql-map-config.xml</value>
</property>
</bean>


<!-- ========================= BUSINESS DEFINITIONS
========================= -->

<!-- Primary business object: default implementation -->
<bean id="eelServiceTarget"
class="com.wfs.eel.services.EelServiceImpl">
<property name="userDao"><ref bean="userDao"/></property>
<property name="customerDao"><ref
bean="customerDao"/></property>
<property name="locationsDao"><ref
bean="locationsDao"/></property>
<property name="vendorsDao"><ref
bean="vendorsDao"/></property>
<property name="currencyDao"><ref
bean="currencyDao"/></property>
<property name="itemcostDao"><ref
bean="itemcostDao"/></property>
<property name="insertinvoiceDao"><ref
bean="insertinvoiceDao"/></property>
<property name="customerinfoDao"><ref
bean="customerinfoDao"/></property>
<property name="insertcustomerDao"><ref
bean="insertcustomerDao"/></property>
<property name="insertlineDao"><ref
bean="insertlineDao"/></property>
<property name="recordDao"><ref
bean="recordDao"/></property>
<property name="accategoryDao"><ref
bean="accategoryDao"/></property>
</bean>

<!-- Transactional proxy for the primary business object -->
<bean id="eelServiceProxy"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBe
an">
<property name="transactionManager"><ref
bean="appTransactionManager"/></property>
<property name="target"><ref
bean="eelServiceTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id="eelService"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.wfs.eel.services.EelService</value>
</property>
<property name="interceptorNames">
<list>
<!-- <value>loggerInterceptor</value> -->
<value>eelServiceProxy</value>
</list>
</property>
</bean>


<!-- ========================= DAO DEFINITIONS: IBATIS
IMPLEMENTATIONS ========================= -->
<bean id="userDao" class="com.wfs.eel.dao.UserSqlMapDao">
<property name="dataSource"><ref
local="appDataSource"/></property>
<property name="sqlMapClient"><ref
local="sqlMapClient"/></property>
</bean>

<bean id="customerDao" class="com.wfs.eel.dao.CustomerSqlMapDao">
<property name="dataSource"><ref
local="appDataSource"/></property>
<property name="sqlMapClient"><ref
local="sqlMapClient"/></property>
</bean>

<bean id="locationsDao" class="com.wfs.eel.dao.LocationsSqlMapDao">
<property name="dataSource"><ref
local="appDataSource"/></property>
<property name="sqlMapClient"><ref
local="sqlMapClient"/></property>
</bean>

<bean id="vendorsDao" class="com.wfs.eel.dao.VendorsSqlMapDao">
<property name="dataSource"><ref
local="appDataSource"/></property>
<property name="sqlMapClient"><ref
local="sqlMapClient"/></property>
</bean>

<bean id="currencyDao" class="com.wfs.eel.dao.CurrencySqlMapDao">
<property name="dataSource"><ref
local="appDataSource"/></property>
<property name="sqlMapClient"><ref
local="sqlMapClient"/></property>
</bean>

<bean id="itemcostDao" class="com.wfs.eel.dao.ItemcostSqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>

<bean id="insertinvoiceDao"
class="com.wfs.eel.dao.InsertinvoiceSqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>


<bean id="customerinfoDao"
class="com.wfs.eel.dao.CustomerinfoSqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>

<bean id="insertcustomerDao"
class="com.wfs.eel.dao.InsertcustomerSqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>

<bean id="insertlineDao"
class="com.wfs.eel.dao.InsertlineSqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>

<bean id="recordDao" class="com.wfs.eel.dao.RecordSqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>

<bean id="accategoryDao"
class="com.wfs.eel.dao.AccategorySqlMapDao">
<property name="dataSource"><ref local="appDataSource"/></property>
<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
</bean>


</beans>


Thank you

irbouho
Sep 14th, 2004, 11:13 AM
It seems like you need to create a new method that calls both insertInvoice and insertLine and apply transaction demarcation to this new method.

skamiset
Sep 14th, 2004, 12:40 PM
Hi Omar Irbouh


Thannk you for the reply I appreciate it very much. Just wondering if u happen to have a code snippet for transaction demarcation. I haven't done that before. I will apprecaite u r help.

The method u suggested should it be in my service class?

Thank you

irbouho
Sep 14th, 2004, 02:46 PM
Just wondering if u happen to have a code snippet for transaction demarcation
There is nothing "magic" in this. Let's reuse transaction configuration from your applicationContext.xml

<bean id="eelServiceProxy"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager">
<ref bean="appTransactionManager"/>
</property>
<property name="target">
<ref bean="eelServiceTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

now in your service class you create a new method


public void newMethod&#40;final Invoice inv, final Line lin&#41; &#123;
insertInvoice&#40;inv&#41;;
insertLine&#40;lin&#41;;
&#125;

This way, insertInvoice and insertLine will participate to the same Spring managed transaction.


The method u suggested should it be in my service class?
As usually it depends on your application design. Putting newMethod inside the same service class should work very well.

HTH