初学spring,编写一个事务的例子,但是不能回滚。代码如下
spring配置文件
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
classpath:config.properties
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${datasource.driverClassName}</value>
</property>
<property name="url">
<value>${datasource.url}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="addUser">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userDao" class="cn.com.home.dao.jdbc.UserDao" autowire="byName"></bean>
<bean id="userServiceTarget" class="cn.com.home.service.UserService" autowire="byName"></bean>
<bean id="userService" parent="txProxyTemplate" >
<property name="target"><ref local="userServiceTarget"/></property>
</bean>
[/code]
dao代码
[code="java"]
public class UserDao {
private JdbcTemplate jdbcTemplate;
/**
* @param jdbcTemplate
* the jdbcTemplate to set
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
*
* @param user
*/
public void save(User user) throws DatabaseException {
String sql = "insert into t_user (id,name,sex,age,city) values (?,?,?,?,?)";
Object[] params = new Object[] { user.getId(), user.getName(),
user.getSex(), user.getAge(), user.getCity() };
try {
jdbcTemplate.update(sql, params);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new DatabaseException(e);
}
}
}
[/code]
service代码
[code="java"]
public class UserService{
private UserDao userDao;
/**
* @param userDao
* the userDao to set
*/
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* @throws Exception
*
*/
public void addUser() throws DatabaseException {
User u = new User();
u.setId(Long.valueOf(123));
u.setName("张三");
u.setAge(20);
u.setSex("男");
u.setCity(571);
userDao.save(u);
User u2 = new User();
u2.setId(Long.valueOf(123));
u2.setName("李四");
u2.setAge(20);
u2.setSex("男男男男男男男男男男男男男男男男男男男男男男男男男男");
u2.setCity(571);
userDao.save(u2);
}
/**
* @param args
*/
public static void main(String[] args) {
BeanFactory bf = new ClassPathXmlApplicationContext(
"spring/applicationContext.xml");
UserService service = (UserService) bf.getBean("userService");
try {
service.addUser();
} catch (DatabaseException e) {
}
}
}
[/code]
第二个user的sex故意设置成超过数据库字段长度的值,故意是inser出错。
但是为什么 这个事务没有回滚呢? 结果是第一个user插入到了数据库中。应该是两个都没有提交的。