日期:2014-05-17  浏览次数:20723 次

奇怪的spring事物
大家好:
  最近想看看spring事物,于是照网上说法跟着做了,可始终事物都没起作用,不知道出了什么问题,大家帮忙看看,
applicatioinContext.xml配置如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="username">
<value>ams</value>
</property>
<property name="password">
<value>ams</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@dong:1521:DYB</value>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="userDao" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="services" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
</props>
</property>
</bean>

<bean id="services" class="com.dyb.text.services.TestServices">
<property name="dao" ref="testdao"></property>
</bean>
<bean id="testdao" class="com.dyb.text.dao.TestDAO">
<property name="source" ref="dataSource"></property>
</bean>
这里面配置了一个service,一个dao,一个事物管理器,一个数据源。
下面是我service代码:
public class TestServices {
private TestDAO dao;

public TestDAO getDao() {
return dao;
}

public void setDao(TestDAO dao) {
this.dao = dao;
}

public void ins(TUser user, TUser updateuser) throws Exception {
dao.update(updateuser);
dao.insert(user);
}
}
其中,ins方法分别调用dao的2个方法,其中update成功,insert失败
下面是我dao的方法
public class TestDAO {
private BasicDataSource source;

public TestDAO() {
}

public BasicDataSource getSource() {
return source;
}

public void setSource(BasicDataSource source) {
this.source = source;
}


public void insert(TUser user) throws Exception {
Connection con;
try {
con = source.getConnection();
PreparedStatement st = con
.prepareStatement("insert into t_user(i,name,sex) values(?,?,?)");
st.setInt(1, user.getId());
st.setString(2, user.getName());
st.setInt(3, user.getSex());
int rs = st.executeUpdate();
st.close();
con.close();
} catch (SQLException e) {
throw e;
}

}

public void update(TUser user) throws Exception {
Connection con;
try {
con = source.getConnection();
PreparedStatement st = con
.prepareStatement("update t_user set name=? where id=?");
st.setString(1, user.getName());
st.setInt(2, user.getId());
int rs = st.executeUpdate();
st.close();
con.close();
} catch (SQLException e) {
throw e;
}
}
}
其中引用了一个数据源,根据数据源来获取连接
下面是我测试方法
ApplicationConte