spring事务不能回滚?
xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>model.User</value>
<value>model.Log</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<aop:config>
<aop:pointcut expression="execution(public * service..*.*(..))"
id="bussinessService" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="getUser" read-only="true" rollback-for="RuntimeException"/>
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
userService.java
@Component
public class UserService {
private LogDAO logDAO;
private UserDAO userDAO;
public void add(User u) {
this.userDAO.save(u);
Log log = new Log();
log.setMsg("a used saved!!");
logDAO.save(log);
}
public LogDAO getLogDAO() {
return logDAO;
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource
public void setLogDAO(LogDAO logDAO) {
this.logDAO = logDAO;
}
@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
LogDAOImpl.java
@Component
public class LogDAOImpl implements LogDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(Log log) {
Session s = sessionFactory.getCurrentSession();
s.save(log);
throw new RuntimeException("error!"); //这里明明抛出了运行时异常,但数据还是插进去了
}
}
事务无法回滚,用注解的也一样,我是看的一个老师的视频敲的代码,我把老师的源码copy进去运行,也还是不行,不知是什么原因啊