下面代码为什么在出现异常时,不能回滚事务?希望帮忙看看。
sql-map-config.xml
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="false" /> <transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://......." />
<property name="JDBC.Username" value="..." />
<property name="JDBC.Password" value="..." />
</dataSource>
</transactionManager> <sqlMap resource="account.xml" /></sqlMapConfig>account.xml
<sqlMap namespace="account"> <select id="getAccount" parameterClass="java.lang.String"
resultClass="example.Account">
select * from account where username = #value#
</select> <insert id="createAccount" parameterClass="example.Account">
insert into account
(username, password)
values ( #username#, #password# )
</insert> <update id="updateAccount" parameterClass="example.Account">
update account set
password = #password# WHERE username = #username#
</update> <delete id="deleteAccount" parameterClass="java.lang.String">
DELETE account WHERE
username = #value#
</delete></sqlMap>Main1.java
public class Main1 { public static void main(String[] args) throws IOException { SqlMapClient sqlMapClient = SqlMapConfig.getSqlMapInstance();
try {
sqlMapClient.startTransaction(); Account account = (Account) sqlMapClient.queryForObject("getAccount", "a");
account.setPassword("aaa");
sqlMapClient.update("updateAccount", account); Account account1 = (Account) sqlMapClient.queryForObject("getAccount", "c");
account1.setPassword("bbb");
sqlMapClient.update("updateAccount", account1); sqlMapClient.commitTransaction(); } catch(SQLException e) {
e.printStackTrace();
} finally {
try {
sqlMapClient.endTransaction();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}上面的代码在执行到Account account1 = (Account) sqlMapClient.queryForObject("getAccount", "c");出现异常,因为表中没有username=c的记录。理论上username=a,更新password=aaa的操作应该是回滚呀。但是我现在都不回滚。希望明白的同学帮帮忙。