浏览 12115 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-08-30
框架:
spring+ibatis 数据库: oracle User表:有userId和userName列 对应的DDL语句:create table user(userId NUMBER(5) primary key,userName VARCHAR2(50)) User类: package model; public class User { // Fields private Integer userid; private String username; public Integer getUserid() { return this.userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } } ibatis配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="User"> <typeAlias alias="user" type="model.User"/> <insert id="saveUser" parameterClass="user"> insert into user(userid,username) values(#userid#,#username#) </insert> </sqlMap> 配置文件(applicationContext.xml): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@192.168.0.200:1521:test</value> </property> <property name="username"> <value>linmin</value> </property> <property name="password"> <value>lm123</value> </property> </bean> <!-- Spring iBatis Template --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="sqlmap-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- UserDAO方案一 --> <bean id="userDAO1" class="dao.UserDAOImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> </beans> DAO接口: package dao; import model.User; public interface UserDAO { public User1 saveUser(User user) throws Exception; } DAO实现类: package dao; import java.sql.SQLException; import model.User; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.ibatis.sqlmap.client.SqlMapClient; public class UserDAOImpl implements UserDAO { protected SqlMapClient sqlMapClient; public User1 saveUser(User user) throws Exception{ try { sqlMapClient.startTransaction(); sqlMapClient.insert("saveUser",user); sqlMapClient.insert("saveUser",user); sqlMapClient.commitTransaction(); return user; } finally{ sqlMapClient.endTransaction(); } } public SqlMapClient getSqlMapClient() { return sqlMapClient; } public void setSqlMapClient(SqlMapClient sqlMapClient) { this.sqlMapClient = sqlMapClient; } } 测试类: package test; import dao.*; import model.*; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class TestDAO1 { public static void main(String[] args) { try{ ClassPathResource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory factory = new XmlBeanFactory(res); User user = new User(1,"guadi"); UserDAO dao = (UserDAO)factory.getBean("userDAO1"); dao.saveUser(user); }catch(Exception e){ e.printStackTrace(); } } } 预期结果: 两语句均回滚,数据库中无记录 结果:数据库中有一条记录 userid:1 username:guadi 即第一条insert语句成功,第二条insert语句失败 这是为什么啊,为什么不能将声明的整个事务回滚,如果可以的话,该如果写? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-08-30
自动代理 <?xml version="1.0" encoding="GB2312"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-lazy-init="true"> <description>自动代理对象下文环境</description> <aop:config> <aop:advisor pointcut="execution(* *..*Logic.*(..))" advice-ref="txAdvice" /> </aop:config> </beans> 事务配置 <?xml version="1.0" encoding="GB2312"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-lazy-init="true"> <description>JDBC事务对象下文环境</description> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 事务要使用的数据源 --> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="add*"/> <tx:method name="modify"/> <tx:method name="delete*"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> </beans> |
|
返回顶楼 | |