spring 事务 不回滚

zhaoyanbin123 2008-09-06 05:59:09
执行中抛出 sql异常 而没有抛出 spring的 dataaccess 异常



<bean id="blServise" class="com.zyb.jsh.bl.BLTestDaoAction">
<property name="commonDao">
<ref bean="CommonDao"/>
</property>
</bean>



<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="JSHSessionFactory"/>
</property>
</bean>





<!-- 利用继承的思想简化配置,要把abstract="true" -->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="test*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
</bean>

<bean id="blServiceProxy" parent="transactionBase">
<property name="target">
<ref local="blServise" />
</property>
</bean>



测试类





package com.zyb.jsh.bl;

import com.zyb.jsh.db.BaseSHDao;
import com.zyb.jsh.db.UserInfo;

public class BLTestDaoAction {

private BaseSHDao commonDao;
private UserInfo user;

public String testDao() {

String loginResult = "failure";

BaseSHDao commonDaoIns = this.getCommonDao();

commonDaoIns.insert(this.user);

//插入同样的数据违反,唯一性约束,抛出异常
commonDaoIns.insert(this.user);

return loginResult;
}

public BaseSHDao getCommonDao() {
return commonDao;
}

public void setCommonDao(BaseSHDao commonDao) {
this.commonDao = commonDao;
}


public UserInfo getUser() {
return user;
}

public void setUser(UserInfo user) {
this.user = user;
}
}


抛出的异常



com.ibm.db2.jcc.a.SqlException: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 2;ADMINISTRATOR.USER
com.ibm.db2.jcc.a.tf.d(tf.java:1397)
com.ibm.db2.jcc.b.jb.l(jb.java:367)
com.ibm.db2.jcc.b.jb.a(jb.java:64)
com.ibm.db2.jcc.b.w.a(w.java:48)
com.ibm.db2.jcc.b.dc.b(dc.java:302)
com.ibm.db2.jcc.a.uf.cb(uf.java:1739)
com.ibm.db2.jcc.a.uf.d(uf.java:2339)
com.ibm.db2.jcc.a.uf.Y(uf.java:544)
com.ibm.db2.jcc.a.uf.executeUpdate(uf.java:527

。。。。。

...全文
379 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fosjos 2008-09-07
  • 打赏
  • 举报
回复
我记得javaeye好像贴出过一个比较实用的service层-dao层的spring配置,和你贴出来的配置有些相似,比较容易理解

楼主该去搜一下
zhaoyanbin123 2008-09-07
  • 打赏
  • 举报
回复
我是按照您文章中的第三种方式配置的啊,这种方式也是依赖注入的也需要cglib的。可就是不成。下面是我debug的一些信息,那个方法根本就没有加入到事务中去。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2008-09-07 15:58:15,546 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
2008-09-07 15:58:15,625 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 5000291514556416
2008-09-07 15:58:15,640 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - saving transient instance
2008-09-07 15:58:15,640 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - saving [com.zyb.jsh.db.UserInfo#<null>]
2008-09-07 15:58:15,640 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - executing insertions
2008-09-07 15:58:15,671 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Inserting entity: com.zyb.jsh.db.UserInfo (native id)
2008-09-07 15:58:15,671 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2008-09-07 15:58:15,671 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
2008-09-07 15:58:15,671 DEBUG [org.hibernate.SQL] - insert into user (name, password, id) values (?, ?, default)Hibernate: insert into user (name, password, id) values (?, ?, default)
2008-09-07 15:58:15,671 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2008-09-07 15:58:15,937 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Dehydrating entity: [com.zyb.jsh.db.UserInfo#<null>]
2008-09-07 15:58:15,937 DEBUG [org.hibernate.type.StringType] - binding 'ff' to parameter: 1
2008-09-07 15:58:15,937 DEBUG [org.hibernate.type.StringType] - binding 'ff' to parameter: 2
2008-09-07 15:58:16,015 DEBUG [org.hibernate.id.IdentifierGeneratorFactory] - Natively generated identity: 5
2008-09-07 15:58:16,015 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2008-09-07 15:58:16,015 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2008-09-07 15:58:16,031 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - flushing session
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - processing flush-time cascades
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - dirty checking collections
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushing entities and processing referenced collections
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Processing unreferenced collections
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Scheduling collection removes/(re)creates/updates
2008-09-07 15:58:16,031 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2008-09-07 15:58:16,046 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2008-09-07 15:58:16,046 DEBUG [org.hibernate.pretty.Printer] - listing entities:
2008-09-07 15:58:16,046 DEBUG [org.hibernate.pretty.Printer] - com.zyb.jsh.db.UserInfo{password=ff,name=ff, id=5}
2008-09-07 15:58:16,046 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - executing flush
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.ConnectionManager] - registering flush begin
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.ConnectionManager] - registering flush end
2008-09-07 15:58:16,046 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - post flush
2008-09-07 15:58:16,046 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
2008-09-07 15:58:16,046 DEBUG [org.hibernate.impl.SessionImpl] - closing session
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.ConnectionManager] - performing cleanup
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction completion
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.ConnectionManager] - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2008-09-07 15:58:16,046 DEBUG [org.hibernate.impl.SessionImpl] - after transaction completion
=====================================================================
2008-09-07 15:58:16,046 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
2008-09-07 15:58:16,046 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 5000291516604416
2008-09-07 15:58:16,046 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - saving transient instance
2008-09-07 15:58:16,046 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - saving [com.zyb.jsh.db.UserInfo#<null>]
2008-09-07 15:58:16,046 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - executing insertions
2008-09-07 15:58:16,046 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Inserting entity: com.zyb.jsh.db.UserInfo (native id)
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
2008-09-07 15:58:16,046 DEBUG [org.hibernate.SQL] - insert into user (name, password, id) values (?, ?, default)Hibernate: insert into user (name, password, id) values (?, ?, default)
2008-09-07 15:58:16,046 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2008-09-07 15:58:16,046 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Dehydrating entity: [com.zyb.jsh.db.UserInfo#<null>]
2008-09-07 15:58:16,046 DEBUG [org.hibernate.type.StringType] - binding 'ff' to parameter: 1
2008-09-07 15:58:16,046 DEBUG [org.hibernate.type.StringType] - binding 'ff' to parameter: 2
2008-09-07 15:58:16,078 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2008-09-07 15:58:16,078 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2008-09-07 15:58:16,093 DEBUG [org.hibernate.util.JDBCExceptionReporter] - could not insert: [com.zyb.jsh.db.UserInfo] [insert into user (name, password, id) values (?, ?, default)]
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry 'ff' for key 2
宋玮-深圳 2008-09-07
  • 打赏
  • 举报
回复
呵呵,上面打错了。
看我这篇blog,详细写了几种事务的配置方法http://blog.csdn.net/d8111/archive/2008/07/19/2678256.aspx
宋玮-深圳 2008-09-07
  • 打赏
  • 举报
回复
DB2 SQL error
zhaoyanbin123 2008-09-07
  • 打赏
  • 举报
回复
BLTestDaoAction 就是我配置了事务的bean啊! 对该bean的test* 方法做代理的啊 !
fosjos 2008-09-07
  • 打赏
  • 举报
回复
既然配置了spring事务,就要用配置了事务的bean
况且,楼主没有理解事务代理的原理,结构有些乱
应该去看看spring示例配置

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧