我最终试图调试此问题。基本上,我有一个mvc应用程序,我有一个BusinessManagerImpl
类,它有2个DAO(UserDao
和ProductDao
),并且使用JDBC连接池而不是ORM。数据库是带有InnoDb引擎的mySQL。 RestUserController
是BusinessManagerImpl
的调用类。
BusinessManagerImpl.addUser()
已使用@Transactional
注释进行了注释。我也试过在班级注释@Transactional
,但似乎没有什么区别。这两个DAO也都是这样注释的。
BusinessManagerImpl.addUser()
使用UserDao
插入用户,但后续调用ProductDao.getAllProducts()
会故意抛出RuntimeException
以使事务回滚。我的期望是用户不应该被插入,因为RuntimeException
已经发生并且事务将被回滚但我已经检查了我的数据库并且插入了新用户。
我尝试过抛出一个已检查的异常并使用@Transactional
注释的“rollback for”参数,但它不起作用。我也尝试了不同的传播值,如Propagation.Required
,但似乎对回滚事务没有影响。我试过在stackoverflow和谷歌搜索,但没有提出任何可能有帮助。有人可以说清楚我做错了什么或遗失了什么吗?谢谢。以下是我的设置:
应用context.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<context:component-scan base-package="someproject" />
<!-- <context:annotation-config /> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/somedb" />
<property name="username" value="xxx" />
<property name="password" value="yyy" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
BusinessManagerImpl类
@Service
public class BusinessManagerImpl implements BusinessManager{
@Autowired
private UserDao userDao;
@Autowired
private ProductDao productDao;
....
@Override
@Transactional(propagation=Propagation.REQUIRED)
public User addUser(User user) throws Exception {
// TODO Auto-generated method stub
User tempUser = userDao.addUser(user);
productDao.getAllProducts();
return tempUser;
}
UserDaoImpl类
@Service
public class UserDaoImpl implements UserDao {
private DataSource dataSource;
@Autowired
public UserDaoImpl(DataSource dataSource) {
super();
setDataSource(dataSource);
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
private JdbcTemplate getJdbcTemplate(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return jdbcTemplate;
}
...
@Override
@Transactional(propagation=Propagation.MANDATORY)
public User addUser(final User user) {
KeyHolder holder = new GeneratedKeyHolder();
final String sql = "insert into user (username, password) "
+ " VALUES (?, ?)";
getJdbcTemplate().update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
int index = 1;
ps.setString(index++, user.getUsername());
ps.setString(index++, user.getPassword());
return ps;
}
}, holder);
int seq = holder.getKey().intValue();
user.setSeq(seq);
return user;
}
ProductDaoImpl类
@Service
public class ProductDaoImpl implements ProductDao {
private DataSource dataSource;
@Autowired
public ProductDaoImpl(DataSource dataSource) {
super();
setDataSource(dataSource);
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
@Transactional(propagation=Propagation.MANDATORY)
public List<Product> getAllProducts() throws Exception {
if(true)
throw new RuntimeException("on purpose");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Product> products = jdbcTemplate.query(
"select * from product",
new ProductRowMapper());
return products;
}
RestUserController类
@RestController
public class RestUserController {
private static Logger logger = LoggerFactory.getLogger(RestUserController.class);
@Autowired
private BusinessManager businessManager;
@RequestMapping(value = "/adduser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createEmployee(@RequestBody User user)
{
logger.debug("adding user:"+user);
User addedUser=null;
try {
addedUser = businessManager.addUser(user);
return new ResponseEntity(addedUser, HttpStatus.CREATED);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Spring3 MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
弹簧的web-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="someproject" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
答案 0 :(得分:0)
我发现在Dao Implementation类上使用@Service注释很奇怪。尝试使用@Repository替换它们,并将rollbackFor = {Exception.class}
添加到所有事务注释中。
答案 1 :(得分:0)
对于2个应用程序上下文配置,您必须以不同方式指定base-package。不要让Web应用程序上下文扫描DAO包。