struts+spring+ibatis事务管理问题?急急急1

dada360778512 2008-06-03 09:38:17
struts+spring+ibatis
dao层用的是spring对ibatis的包装类,如下:
ScientistDAOImpl extends SqlMapClientDaoSupport implements ScientistDAO
当然事务管理由spring来处理。
以下是spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<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">


<!-- ========================= GENERAL DEFINITIONS ========================= -->

<!-- Configurer that replaces ${...} placeholders with values from properties files -->
<!-- (in this case, mail and JDBC related properties) -->
<!-- serviceBean -->
<bean id="changingService" class="cn.edu.ysu.spring.service.ChangingService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="contractService" class="cn.edu.ysu.spring.service.ContractService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="projectService" class="cn.edu.ysu.spring.service.ProjectService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="reportService" class="cn.edu.ysu.spring.service.ReportService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="scientistService" class="cn.edu.ysu.spring.service.ScientistService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/kyproject"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>

<!-- 管理ibatis的事务,加载sql-map-config.xml文件 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" >
<value>
classpath:cn\edu\ysu\dao\sqlmap\sql-map-config.xml
</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- daoBean -->
<bean id="changingDAOImpl" class="cn.edu.ysu.dao.daoImplement.ChangingDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="contractDAOImpl" class="cn.edu.ysu.dao.daoImplement.ContractDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="projectDAOImpl" class="cn.edu.ysu.dao.daoImplement.ProjectDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="reportDAOImpl" class="cn.edu.ysu.dao.daoImplement.ReportDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>

<bean id="scientistDAOImpl" class="cn.edu.ysu.dao.daoImplement.ScientistDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>


<!-- 代理,事务管理器,定义事务 -->
<bean id="daoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>
<property name="target">
<list>
<ref local="scientistService"/>
<ref local="changingService"/>
<ref local="contractService"/>
<ref local="projectService"/>
<ref local="reportService"/>
</list>
</property>
<property name="transactionAttributes" >
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="to*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<!-- 配置事务特性 -->
<!--
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
-->
<!-- 配置哪些类的方法需要事务 -->
<!--
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* cn.edu.ysu.spring.service.ServiceManagerImpl.getScientistService(..))"/>
<aop:aspect id="other" ref="">
<aop:before pointcut-ref="allMethod" method="other"/>
</aop:aspect>
</aop:config>

-->
</beans>
现在的问题是:
好像spring把事务管理放在dao层了,应该放在service层,我也想这么做
例如:
public class ScientistService extends BaseService{
.....部分代码
public void createScientist(Scientist scientist)
{
scientistDao.kyInsert(scientist);
scientist.setBirthday("111");//把日期改成非法数据
scientistDao.kyInsert(scientist);

}
}
如实,在执行下一个插入操作,应抛出数据库异常,单数据库中存有第一条记录
我想把上面那条记录也会滚,即整个createScientist()方法放在一个事务里
应怎样改?
...全文
1668 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
雨天wxp 2010-04-03
这个可以解决:
1).配置事务通知

<tx:advice id="transactionManagerAdivice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"
isolation="READ_COMMITTED"
propagation="REQUIRED"
rollback-for="java.lang.RuntionException" />
</tx:attributes>
</tx:advice>

2).配置切入点和方面

<aop:config>
<aop:pointcut expression="execution(* com.hj.bankOps.DefaultBankService.*(..))" id="bankServicePc"/>
<aop:advisor advice-ref="transactionManagerAdivice" pointcut-ref="bankServicePc"/>
</aop:config>
回复
jaguar_yang 2010-01-29
声明式事务好像做不到,只能用代码提交事务。
回复
myqmanager 2009-12-30
同样的问题哪,有人回答吗??
回复
pentium1512 2008-06-13
迫切想知道答案,帮你顶
回复
dada360778512 2008-06-04
继续顶,希望高人来回答
回复
dada360778512 2008-06-04
自己先顶一下
回复
tcl_79527922 2008-06-03
其实本来事务的管理都是注入在服务层的不应该在DAO层的。。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2008-06-03 09:38
社区公告
暂无公告