标签:
一.Spring配置文件如下:
<bean id="test" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.1.192:1521:test" />
<property name="username" value="test" />
<property name="password" value="test" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
<!--transactionManager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="test" />
</bean>
<bean id="baseTxProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="test_jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="test" />
</property>
</bean>
<!-- manage Dao -->
<bean id="testImpDao"
class="com.test.dao.TestImpDao">
<property name="jdbcTemplate">
<ref bean="test_jdbcTemplate" />
</property>
</bean>
<!-- transaction services -->
<bean id="testService" parent="baseTxProxy">
<property name="target">
<bean
class=com.test.service.TestServiceImp">
<property name="testImpDao">
<ref bean="testImpDao" />
</property>
</bean>
</property>
</bean>
二.实现类简单说明:
1.DAO的接口
public interface ITestDAO {
public void insertTable1();
public void insertTable2();
public void insertTable3();
}
2.DAO的实现
public class ImpTestDAO implements ITestDAO{
public void insertTable1(){
this.getJdbcTemplate().update(sb.toString(),paraObjectArray);
}
public void insertTable2(){
this.getJdbcTemplate().update(sb.toString(),paraObjectArray);
}
public void insertTable3(){
this.getJdbcTemplate().update(sb.toString(),paraObjectArray);
}
}
3.service的接口层:
public interface ITestService {
public String saveOperate(String params);
}
4.service的实现层
public class TestServiceImp implements ITestService{
private TestDAO testDAO;
public void setTestDAO(TestDAO testDAO){
this.testDAO=testDAO;
}
public void saveOperate(){
this.testDAO.insertTable1();
this.testDAO.insertTable2();
this.testDAO.insertTable3();
}
}
5.前台的调用,如:aciton层l
public String saveOperate(String params) {
String returnStr = "";
StringBuffer errorSb = new StringBuffer("[");
try {
WebApplicationContext appContext=WebApplicationContextUtils.
getWebApplicationContext(this.getServlet().getServletContext());
ITestService Service = (ITestService) appContext
.getBean("testService");
returnStr = Service.saveOperate(params);
}// 如果执行失败,把具体的异常信息输出,并且回滚相应的事务;
catch (Exception e) {
errorSb.append("{result:error,");
errorSb.append("info:"" + e.getMessage() + ""}");
errorSb.append("]");
returnStr = errorSb.toString();
}
return returnStr;
}
碰到的问题:在DAO的实现层,把jdbc的操作异常抛到service的实现层--即:‘TestServiceImp‘类中时,
自己写了try{}catch(){},在方法中把异常给处理了,出现事务无法回滚的错误。
备注: 经过调试,发现TestDaoImp中的JDBC操作时,当执行出错时不需要对异常进行特殊处理,
而是应该把异常抛出到service的实现类中后,在service的实现类中也不需要对该异常出处理,
而是要把该异常抛出调用serviceImp的类中(如:相应的aciton),这样
<bean id="testService" parent="baseTxProxy">,这个代理类才能捕获到JDBC抛出的异常,才能根据对应的异常进行判断是否要进行事务的回滚操作.
标签:
原文地址:http://www.cnblogs.com/whtydn/p/5125363.html