大家好,我最近几天一直在调spring事务.可事务不回滚的问题一直困绕着我.
说一下我的大概思路,在Service层,分别调用两个DAO层的业务方法.实现保存功能.
我采用的数据库是Sql Server2000
Service层代码:
public class UserService implements IUserService {
private IUserDAO userDAO;
private IAddressDAO addressDAO;
public void saveUserAndAddress(User user, Address address) {
//我想要实现的效果: 当userDAO执行成功,并且addressDAO执行成功,则数据库中对应USER表,及ADDRESS保存数据.
//如果userDAO执行失败,或addressDAO执行失败,则USER表,及ADDRESS表不保存数据
userDAO.saveUser(user);
addressDAO.saveAddress(address);
}
//下面的get / set userDAO , addressDAO方法略
....
UserDAO层代码:
public class UserDAOImpl implements IUserDAO {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveUser(User user) {
String sql = "insert into ma_user_info (id , user_name , pass_word) values("+user.getId()+",'"+user.getUsername()+"' ,'"+user.getPassword()+"')";
jdbcTemplate.update(sql);
}
}
AddressDAO层代码:
public class AddressDAOImpl implements IAddressDAO {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveAddress(Address address) {
String sql = "insert into ma_address_info (i,address)values("+address.getId()+",'"+address.getAddName()+"')";;
jdbcTemplate.update(sql) ;
}
}
beans-config.xml配置:
<bean id="transactionDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=hibernate_research" />
<property name="username" value="sa" />
<property name="password" value="sa" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg ref="transactionDataSource" />
</bean>
<!-- data access layer -->
<bean id="transactionUserDAO" class="com.transaction.dao.user.UserDAOImpl" >
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="transactionAddressDAO" class="com.transaction.dao.address.AddressDAOImpl" >
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- business logic layer -->
<bean id="transactionUserService" class="com.transaction.service.UserService">
<property name="userDAO" ref="transactionUserDAO" />
<property name="addressDAO" ref="transactionAddressDAO" />
</bean>
<!-- proxy -->
<bean id="transactionUserServiceProxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.transaction.service.IUserService</value>
</list>
</property>
<property name="target" ref="transactionUserService" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
</props>
</property>
</bean>
最后的main方法测试类代码:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
IUserService userService = (IUserService)context.getBean("transactionUserServiceProxyBean");
User user = new User();
user.setId(100);
user.setUsername("username_100");
user.setPassword("password_100");
Address address = new Address();
address.setId(100);
address.setAddName("address_100");
userService.saveUserAndAddress(user, address);
}
请各位不吝指教.万分感谢!