Hi,
I am trying to use TransactionProxyFacotryBean to implement declarative transaction management, however I noticed that my inserts get executed and committed as soon as the jdbc statement is executed via the jdbc template. I am newbie and am not sure what I am doing wrong. Even when my target class throws an exception the data still shows up in the database.
Thanks,
Sohil
The configuration file is below..
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@SERVER:1521:INSTANCE</value>
</property>
<property name="username"><value>USERNAME</value></property>
<property name="password"><value>PASSWORD</value></property>
</bean>
<bean id="txdataSource" class="org.springframework.jdbc.datasource.Transac tionAwareDataSourceProxy">
<constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>
<!-- transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
<!-- proxy calls to the manager to automatically manage the transaction -->
<!-- TODO extend and and add property for commit strategy -->
<bean id="sampleManagerTransactionProxy" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
<property name="proxyTargetClass"><value>true</value></property>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-TestException</prop>
</props>
</property>
<property name="target"><ref local="sampleMainManager"/></property>
</bean>
<!-- main manager responsible for staging and processing -->
<bean id="sampleMainManager" class="manager.SampleMainManager">
<property name="ds"><ref local="txdataSource"/></property>
</bean>
and the java code does the following. I first tried supplying the datasource directly to the samplemainmanager and then using the transactionAwareDataSourceProxy but neither works. I execute the process method on the SampleMainManager after obtaining an instance from the ApplicationContext.
public class SampleMainManager
{
TransactionAwareDataSourceProxy ds;
//DataSource ds;
public void setDs (TransactionAwareDataSourceProxy ds)
{
this.ds = ds;
}
public void process () throws Exception
{
try{
if(ds != null){
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(ds);
jt.execute("insert into temp (id) values(2)");
}else{
System.out.println("datasource is null");
}
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
}


Reply With Quote