Spring3.0 用注解进行事务管理 但是事务回滚不了

chenyuqing2012 2011-10-31 06:55:48


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config/>
<context:component-scan base-package="cn.com.utt"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>

<bean id="parentDataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="mysqlDataSource" parent="parentDataSource">
<property name="url" value="${mysql.url}"/>

</bean>
<bean id="infobrightDataSource" parent="parentDataSource">
<property name="url" value="${infobright.url}" />
</bean>
<bean id="dataSource" class="cn.com.utt.source.RoutingDataSource">
<property name="targetDataSources">
<map key-type="cn.com.utt.source.DataSourceType">
<entry key="MYSQLSOURCE" value-ref="mysqlDataSource"/>
<entry key="INFOBRIGHTSOURCE" value-ref="infobrightDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="mysqlDataSource"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>

<!-- pojo映射文件 -->
<property name="annotatedClasses">
<list>
<value>cn.com.utt.pojo.Reportplot</value>
<value>cn.com.utt.pojo.Report</value>
<value>cn.com.utt.pojo.Equipstatus</value>
<value>cn.com.utt.pojo.Intfstatus</value>
<value>cn.com.utt.pojo.User</value>
<value>cn.com.utt.pojo.Emailserver</value>
<value>cn.com.utt.pojo.Receiver</value>
<value>cn.com.utt.pojo.TreeNode</value>
<value>cn.com.utt.pojo.UserNode</value>
<value>cn.com.utt.pojo.Equip</value>
<value>cn.com.utt.pojo.Smsserver</value>
<value>cn.com.utt.pojo.Monitortype</value>
<value>cn.com.utt.pojo.Alarmlog</value>
<value>cn.com.utt.pojo.Bakplot</value>
<value>cn.com.utt.pojo.EquipBak</value>
<value>cn.com.utt.pojo.ReportplotEquip</value>
<value>cn.com.utt.pojo.QuartzJob</value>
<value>cn.com.utt.pojo.Alarmplot</value>
<value>cn.com.utt.pojo.Alarmrule</value>
<value>cn.com.utt.pojo.AlarmEquip</value>
<value>cn.com.utt.pojo.AlarmrulePlot</value>
<value>cn.com.utt.pojo.AlarmruleReceiver</value>
<value>cn.com.utt.pojo.Equiplog</value>
<value>cn.com.utt.pojo.Mib</value>
<value>cn.com.utt.pojo.Config</value>
<value>cn.com.utt.pojo.ConfigDetail</value>
<value>cn.com.utt.pojo.Urllog</value>
<value>cn.com.utt.pojo.Natlog</value>
<value>cn.com.utt.pojo.IMlog</value>
<value>cn.com.utt.vo.UrlRank</value>
<value>cn.com.utt.pojo.Emaillog</value>
<value>cn.com.utt.pojo.BBSlog</value>
<value>cn.com.utt.pojo.Weibolog</value>
<value>cn.com.utt.pojo.AlarmCount</value>
<value>cn.com.utt.pojo.UrlCount</value>
<value>cn.com.utt.vo.DatasetValue</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

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

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="addUserNode" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="list*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="copy*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="false" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut expression="execution(public * cn.com.utt.dao..*.*(..))" id="daopoint"/>
<aop:advisor pointcut-ref="daopoint" advice-ref="txAdvice"/>
</aop:config>

<!-- quartz配置 -->

<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="configLocation" value="classpath:quartz.properties"/><!--
这个是必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动
--><property name="startupDelay" value="30"/><!--
这个是可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
--><property name="overwriteExistingJobs" value="true"/>
<property name="jobDetails" >
<list>
<ref bean="jobDetail"/>
</list>
</property>
</bean>
<!--<bean id="jobDetail" class="frameworkx.springframework.scheduling.quartz.BeanInvokingJobDetailFactoryBean">
shouldRecover属性为true,则当Quartz服务被中止后,再次启动任务时会尝试恢复执行之前未完成的所有任务
<property name="shouldRecover" value="true"/>
<property name="targetBean" value="backUpConfigAction"/>
<property name="targetMethod" value="run"/>-->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="cn.com.utt.action.ExecuteJobAction" />
<property name="jobDataAsMap">
<map>
<entry key="jobService">
<ref bean="jobService"/>
</entry>
</map>
</property>
</bean>
</beans>



XML配置


@Component("testDao")
@Transactional
public class TestDaoImpl implements TestDao{

private HibernateTemplate hibernateTemplate;



public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}


@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
public void addReportplot(Reportplot reportplot) throws DataAccessException {
System.out.println("添加DAO");
this.hibernateTemplate.saveOrUpdate(reportplot);

}


@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
public void updateReportplot(Report reportplot)
throws DataAccessException {
System.out.println("修改DAO");
this.hibernateTemplate.saveOrUpdate(reportplot);
}

}



DAO层代码


@Component("testService")
public class TestServiceImpl implements TestService{

private TestDao testDao;



public TestDao getTestDao() {
return testDao;
}
@Resource
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}

public void addReportplot(Reportplot reportplot) {
System.out.println("添加");
testDao.addReportplot(reportplot);
}

public void updateReportplot(Report reportplot) {
System.out.println("修改");
testDao.updateReportplot(reportplot);
}




service 层代码


@Component("testAction")
@Scope("prototype")
public class TestAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

private TestService testService;


public TestService getTestService() {
return testService;
}

@Resource
public void setTestService(TestService testService) {
this.testService = testService;
}


public String execute()throws Exception{
try {
System.out.println("进入测试");
Reportplot reportplot1 = new Reportplot();
reportplot1.setName("aaaaa");
reportplot1.setStatus((byte)1);
reportplot1.setType((byte)1);
reportplot1.setCreator((long)1);
testService.addReportplot(reportplot1);

Report reportplot2 = new Report();
reportplot2.setId((long)1);
reportplot2.setReportplotid((long)1);
testService.updateReportplot(reportplot2);
} catch (Exception e) {
System.out.println("失败");
// throw new RuntimeException("aaaaaaa");
}
return "success";
}


}






action代码


各位高手高高手指导指导啊。。
...全文
1752 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
microhardware2005 2014-01-14
  • 打赏
  • 举报
回复
@Transactional 应该加在service,而不是DAO!!!!
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance".....>

<context:annotation-config/>
<context:component-scan base-package="cn.com.utt"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>

<bean id="parentDataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="mysqlDataSource" parent="parentDataSource">
<property name="url" value="${mysql.url}"/>

</bean>
<bean id="infobrightDataSource" parent="parentDataSource">
<property name="url" value="${infobright.url}" />
</bean>
<bean id="dataSource" class="cn.com.utt.source.RoutingDataSource">
<property name="targetDataSources">
<map key-type="cn.com.utt.source.DataSourceType">
<entry key="MYSQLSOURCE" value-ref="mysqlDataSource"/>
<entry key="INFOBRIGHTSOURCE" value-ref="infobrightDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="mysqlDataSource"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>

<!-- pojo映射文件 -->
<property name="annotatedClasses">
<list>
<value>cn.com.utt.pojo.Reportplot</value>
<value>cn.com.utt.pojo.Report</value>
<value>cn.com.utt.pojo.Equipstatus</value>
<value>cn.com.utt.pojo.Intfstatus</value>
<value>cn.com.utt.pojo.User</value>
<value>cn.com.utt.pojo.Emailserver</value>
<value>cn.com.utt.pojo.Receiver</value>
<value>cn.com.utt.pojo.TreeNode</value>
<value>cn.com.utt.pojo.UserNode</value>
<value>cn.com.utt.pojo.Equip</value>
<value>cn.com.utt.pojo.Smsserver</value>
<value>cn.com.utt.pojo.Monitortype</value>
<value>cn.com.utt.pojo.Alarmlog</value>
<value>cn.com.utt.pojo.Bakplot</value>
<value>cn.com.utt.pojo.EquipBak</value>
<value>cn.com.utt.pojo.ReportplotEquip</value>
<value>cn.com.utt.pojo.QuartzJob</value>
<value>cn.com.utt.pojo.Alarmplot</value>
<value>cn.com.utt.pojo.Alarmrule</value>
<value>cn.com.utt.pojo.AlarmEquip</value>
<value>cn.com.utt.pojo.AlarmrulePlot</value>
<value>cn.com.utt.pojo.AlarmruleReceiver</value>
<value>cn.com.utt.pojo.Equiplog</value>
<value>cn.com.utt.pojo.Mib</value>
<value>cn.com.utt.pojo.Config</value>
<value>cn.com.utt.pojo.ConfigDetail</value>
<value>cn.com.utt.pojo.Urllog</value>
<value>cn.com.utt.pojo.Natlog</value>
<value>cn.com.utt.pojo.IMlog</value>
<value>cn.com.utt.vo.UrlRank</value>
<value>cn.com.utt.pojo.Emaillog</value>
<value>cn.com.utt.pojo.BBSlog</value>
<value>cn.com.utt.pojo.Weibolog</value>
<value>cn.com.utt.pojo.AlarmCount</value>
<value>cn.com.utt.pojo.UrlCount</value>
<value>cn.com.utt.vo.DatasetValue</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

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

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="addUserNode" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="list*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="copy*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="false" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut expression="execution(public * cn.com.utt.dao..*.*(..))" id="daopoint"/>
<aop:advisor pointcut-ref="daopoint" advice-ref="txAdvice"/>
</aop:config>

<!-- quartz配置 -->

<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="configLocation" value="classpath:quartz.properties"/><!--
这个是必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动
--><property name="startupDelay" value="30"/><!--
这个是可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
--><property name="overwriteExistingJobs" value="true"/>
<property name="jobDetails" >
<list>
<ref bean="jobDetail"/>
</list>
</property>
</bean>
<!--<bean id="jobDetail" class="frameworkx.springframework.scheduling.quartz.BeanInvokingJobDetailFactoryBean">
shouldRecover属性为true,则当Quartz服务被中止后,再次启动任务时会尝试恢复执行之前未完成的所有任务
<property name="shouldRecover" value="true"/>
<property name="targetBean" value="backUpConfigAction"/>
<property name="targetMethod" value="run"/>-->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="cn.com.utt.action.ExecuteJobAction" />
<property name="jobDataAsMap">
<map>
<entry key="jobService">
<ref bean="jobService"/>
</entry>
</map>
</property>
</bean>
</beans>




@Component("testDao")
@Transactional
public class TestDaoImpl implements TestDao{

private HibernateTemplate hibernateTemplate;



public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}


@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class,DataAccessException.class})
public void addReportplot(Reportplot reportplot) throws DataAccessException {
System.out.println("添加DAO");
this.hibernateTemplate.saveOrUpdate(reportplot);

}


@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class,DataAccessException.class})
public void updateReportplot(Report reportplot)
throws DataAccessException {
System.out.println("修改DAO");
this.hibernateTemplate.saveOrUpdate(reportplot);
}

}




@Component("testService")
public class TestServiceImpl implements TestService{

private TestDao testDao;



public TestDao getTestDao() {
return testDao;
}
@Resource
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}

public void addReportplot() throws BusinessException {

try {

Reportplot reportplot1 = new Reportplot();
reportplot1.setName("aaaaa");
reportplot1.setStatus((byte)1);
reportplot1.setType((byte)1);
reportplot1.setCreator((long)1);
System.out.println("添加");
testDao.addReportplot(reportplot1);


Report reportplot2 = new Report();
reportplot2.setId((long)1);
reportplot2.setReportplotid((long)1);
System.out.println("修改");
testDao.updateReportplot(reportplot2);
} catch (Exception e) {

throw new BusinessException("addReportplot方法出错", e);
}
}



}




@Component("testAction")
@Scope("prototype")
public class TestAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

private TestService testService;


public TestService getTestService() {
return testService;
}

@Resource
public void setTestService(TestService testService) {
this.testService = testService;
}


public String execute()throws Exception{
try {
System.out.println("进入测试");

testService.addReportplot();
} catch (Exception e) {
System.out.println("失败");
}
return "success";
}

}

现在的各层的代码
我再说一下我要达到的要求:add方法添加数据成功 update方法修改数据失败,系统抛出错位 add添加的数据也会回滚 数据库没有添加进去大数据
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复
额..我试一试..我其他的代码有问题嚒??
edwin_zhao 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 edwin_zhao 的回复:]

在service层里,去掉try...catch...的包围,或者在catch块中再抛出捕获到的异常
如果你在service里处理掉异常了,就认为是成功执行,不回滚了
[/Quote]

是我审题不清,看错了,看service代码看到action里去了...

要满足你的要求,你需要将add和update放到一个service方法里去
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复

public void updateReportplot(Report reportplot) throws BusinessException{

try {
System.out.println("修改");
testDao.updateReportplot(reportplot);
} catch (Exception e) {

throw new BusinessException("countAllAlarmReport方法出错", e);
}

}



BusinessException 是自定义大异常捕获方法
edwin_zhao 2011-11-01
  • 打赏
  • 举报
回复
直接抛你原本捕获到的异常,对异常的处理放到action里进行
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复
在catch里面抛出运行时异常??
throw new RuntimeException("aaaaaaa");
edwin_zhao 2011-11-01
  • 打赏
  • 举报
回复
在service层里,去掉try...catch...的包围,或者在catch块中再抛出捕获到的异常
如果你在service里处理掉异常了,就认为是成功执行,不回滚了
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复
我要达到的效果是add方法成功 update方法失败 add也会回滚
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复
是default-storage-engine=INNODB
edwin_zhao 2011-11-01
  • 打赏
  • 举报
回复
先看看你操作的表用的存储引擎是哪个
如果用的是不支持事务的存储引擎,如MyISAM,那在spring中怎么配置都没用的
chenyuqing2012 2011-11-01
  • 打赏
  • 举报
回复

嚒人....
chenyuqing2012 2011-10-31
  • 打赏
  • 举报
回复
自己顶....泪奔..呜呜..

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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