Spring ApplicationContext 配置文件: <!-- 定义DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@10.132.224.203:1521:myorcl" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>
<bean id="sqlmapclient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:com/test/ibatis/sqlmap/sqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean> <!-- Ibatis DAO 实现类 -->
<bean id="iBatisDao" class="com.test.dao.impl.UserImpl">
<property name="sqlMapClient">
<ref local="sqlmapclient" />
</property>
</bean>
<!-- Struts Action类 注入业务类 -->
<bean id="useraction" class="com.test.struts.action.UserAction">
<property name="user" ref="user_service"></property>
</bean> <!-- 业务类 注入dao实现类 -->
<bean id="user_service" class="com.test.struts.service.UserService">
<property name="dao" ref="iBatisDao"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManage"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 装配事务 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="user_service"></property>
<property name="transactionManager" ref="transactionManage"></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,-MyException,-RemoteException,-SocketException</prop>
</props>
</property>
</bean>
其中涉及的重要类:
UserImpl 数据层实现类: public class UserImpl extends SqlMapClientDaoSupport implements IUserDao { public void addUserInfo() {
UserModel umodel = new UserModel();
umodel.setId("4");
umodel.setName("lalala");
umodel.setPassword("123456789");
umodel.setState(1);
this.getSqlMapClientTemplate().insert(StaticSqlString.getADD_USER(), umodel);
//故意再插入一条数据导致错误
this.getSqlMapClientTemplate().insert(StaticSqlString.getADD_USER(), umodel);
} public List getUserInfo() {
List list = null;
list = this.getSqlMapClientTemplate().queryForList(StaticSqlString.getSELECT_ALL_USER());
System.out.println(list.size());
return list;
}
}UserService 业务类(事务控制): public List getUserInfo()
{
List list = null;
list = dao.getUserInfo();
return list;
}
public void addUser()throws RuntimeException{
try{
dao.addUserInfo();
}catch(DataAccessException es){
System.out.println("错误了");
//抛出自定义错误MyException,继承自RuntimeException
throw new MyException(es.toString());
}
}
MyException:自定义错误类: public class MyException extends RuntimeException{
public MyException(String msg){
super(msg);
}
}UserAction 中调用业务类方法新增数据: public String execute() throws Exception {
String type = request.getParameter("type").toString();
if(type.equals("1")){
List list = new ArrayList();
list = user.getUserInfo();
this.setUserlist(list);
}else{
user.addUser();
}
return "testok";
}
但不知道为什么在Spring中装配的声明事务似乎同空气一般不起任何作用,不知道是哪里设置错误还是代码上出错了,我有些怀疑是事务控制层对应不上事务装配中指定的类,但又不是非常确定,还请高手帮忙看看是哪里的问题了?多谢多谢!!
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@10.132.224.203:1521:myorcl" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>
<bean id="sqlmapclient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:com/test/ibatis/sqlmap/sqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean> <!-- Ibatis DAO 实现类 -->
<bean id="iBatisDao" class="com.test.dao.impl.UserImpl">
<property name="sqlMapClient">
<ref local="sqlmapclient" />
</property>
</bean>
<!-- Struts Action类 注入业务类 -->
<bean id="useraction" class="com.test.struts.action.UserAction">
<property name="user" ref="user_service"></property>
</bean> <!-- 业务类 注入dao实现类 -->
<bean id="user_service" class="com.test.struts.service.UserService">
<property name="dao" ref="iBatisDao"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManage"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 装配事务 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="user_service"></property>
<property name="transactionManager" ref="transactionManage"></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,-MyException,-RemoteException,-SocketException</prop>
</props>
</property>
</bean>
其中涉及的重要类:
UserImpl 数据层实现类: public class UserImpl extends SqlMapClientDaoSupport implements IUserDao { public void addUserInfo() {
UserModel umodel = new UserModel();
umodel.setId("4");
umodel.setName("lalala");
umodel.setPassword("123456789");
umodel.setState(1);
this.getSqlMapClientTemplate().insert(StaticSqlString.getADD_USER(), umodel);
//故意再插入一条数据导致错误
this.getSqlMapClientTemplate().insert(StaticSqlString.getADD_USER(), umodel);
} public List getUserInfo() {
List list = null;
list = this.getSqlMapClientTemplate().queryForList(StaticSqlString.getSELECT_ALL_USER());
System.out.println(list.size());
return list;
}
}UserService 业务类(事务控制): public List getUserInfo()
{
List list = null;
list = dao.getUserInfo();
return list;
}
public void addUser()throws RuntimeException{
try{
dao.addUserInfo();
}catch(DataAccessException es){
System.out.println("错误了");
//抛出自定义错误MyException,继承自RuntimeException
throw new MyException(es.toString());
}
}
MyException:自定义错误类: public class MyException extends RuntimeException{
public MyException(String msg){
super(msg);
}
}UserAction 中调用业务类方法新增数据: public String execute() throws Exception {
String type = request.getParameter("type").toString();
if(type.equals("1")){
List list = new ArrayList();
list = user.getUserInfo();
this.setUserlist(list);
}else{
user.addUser();
}
return "testok";
}
但不知道为什么在Spring中装配的声明事务似乎同空气一般不起任何作用,不知道是哪里设置错误还是代码上出错了,我有些怀疑是事务控制层对应不上事务装配中指定的类,但又不是非常确定,还请高手帮忙看看是哪里的问题了?多谢多谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货