****Spring 配置文件:****
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<!-- 注册驱动配置 -->
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:serc</value>
</property>
<property name="username">
<value>serc</value>
</property>
<property name="password">
<value>serc</value>
</property>
</bean>
<!-- 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<!--abstract="true"时,该bean不会被实例化 lazy-init="true"的bean,IoC容器启动时不会实例化该bean,只有当容器需要用到时才实例化它。lazy-init有利于容器效率,对于不需要的bean可以先不管 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="/register" class="com.x1ngms.action.UserBeanAction" scope="prototype">
<property name="userBeanBiz">
<ref bean="userBeanBiz"/>
</property>
</bean>
<bean name="userBeanBiz" class="com.x1ngms.biz.UserBeanBizImpl">
<property name="userBeanDao">
<ref bean="userBeanDao"/>
</property>
</bean>
<bean name="userBeanDaoTarget" class="com.x1ngms.dao.UserBeanDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="userBeanDao" parent="transactionBase">
<property name="target">
<ref bean="userBeanDaoTarget"/>
</property>
</bean>
DAO代码:
public class UserBeanDaoImpl extends JdbcDaoSupport implements UserBeanDao {
//数据源方式
public void addUserBean(UserBean userBean) {
try {
String sql = "insert into user_test(id,name,lo1ginName,password)values(SEQ_USER_TEST_ID.NEXTVAL,'"+userBean.getName()+"','"+userBean.getLoginName()+"','"+userBean.getPassword()+"')";
this.getJdbcTemplate().execute(sql);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public List<UserBean> getList() {
try {
String sql = "select id,name,loginName,password from user_test order by id ";
return (List<UserBean>)this.getJdbcTemplate().queryForList(sql);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
//return null;
}
public void deleteUser(String id){
try {
String sql = "delete from user_test where id ="+id;
this.getJdbcTemplate().execute(sql);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
在 Spring 中配置事务时,如果事务没有回滚,可能是以下几个原因:
在你的代码中,可能存在以上原因导致事务不回滚。你可以尝试检查这些地方,看看是否存在问题。