spring事务不回滚

05-05-30 Toby

Spring 1.1.5 + MySql 4.1,采用编程型事务,在insertUser方法中执行两条insert语句,第一条成功执行,第二条抛出异常,按理说事物应该回滚,把第一条插入的要取消,但打开数据库却发现没有取消,请各位大虾看看那里弄错了。代码如下:

public class UserDAO implements IUserDAO {

private DataSource dataSource;

private DataSourceTransactionManager transactionManager;

public void setDataSource(DataSource dataSource){

this.dataSource = dataSource;

}

public void setTransactionManager(DataSourceTransactionManager transactionManager){

this.transactionManager = transactionManager;

}

public void insertUser() throws DataAccessException{

TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);

transactionTemplate.execute(new TransactionCallback(){

public Object doInTransaction(TransactionStatus ts){

JdbcTemplate jdbcTemplate = new JdbcTemplate();

jdbcTemplate.setDataSource(dataSource);

jdbcTemplate.execute("Insert into User (FirstName,LastName) values('123','321')");

jdbcTemplate.execute("Insert into User (FirstName1,LastName) values('333','444')");

//表User中无FirstName1自段,人为制造一个异常

return null;

}

});

}

}

执行时,异常如下

org.springframework.jdbc.BadSqlGrammarException: Bad SQL grammar [Insert into User (FirstName1,LastName) values('333','444')] in task 'executing StatementCallback'; nested exception is java.sql.SQLException: Unknown column 'FirstName1' in 'field list'

org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:198)

org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:262)

org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:283)

com.ce.spring.UserDAO$1.doInTransaction(UserDAO.java:86)

org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:114)

com.ce.spring.UserDAO.insertUser(UserDAO.java:81)

org.apache.jsp.index_jsp._jspService(index_jsp.java:74)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

1
banq
2005-05-30 20:00
检查一下你的DataSource设置。

YuLimin
2006-04-20 17:49
是你的MySQL数据库不支持事务,要安装为支持事务的数据库才行的:)

猜你喜欢