spring事務不回滾

Toby發表於2005-05-30

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)





相關文章