1 .启动器配置开放事务。
package com.mingde; importorg.spring framework.boot.spring应用程序; importorg.spring framework.boot.auto configure.springbootapplication; importorg.spring framework.transaction.annotation.enabletransaction management; 启动@ springbootapplication//spring APP应用程序@EnableTransactionManagement //注释事务管理,启动xml部署方式的tx : annotation-drivivery 与publicclasspringbootdemo { publicstaticvoidmain [ ] args } { spring application }等效}2.在服务中配置事务管理器
package com.Ming de.services.impl; import java.util.List; importorg.spring framework.beans.factory.annotation.auto wired; importorg.spring framework.stereotype.service; importorg.spring framework.transaction.annotation.propagation; importorg.spring framework.transaction.annotation.transactional; import com.Ming de.Dao.student Dao; import com.mingde.po.Student; import com.Ming de.services.studentservice; @ servicepublicclassstudentserviceimplimplementsstudentservice { @ autowiredprivatestudentdaostudentdao; @Override//@Transactional只有这样写,默认值为只读。 以下写法是@ transactional (propagation=propagation.required ) /用于添加或删除必需的事务,并在发生错误时回滚事务的publi clililiity
注意:
1 .默认情况下遇到thrownewruntimeexception(“…”; 可以回滚
需要捕获的thrownewexception(「…”); 不回滚
2 .有工作的话,就把工作放进去。 没有的话就新建。 (如果不写)。
@ transactional (传播=传播. required )容器不会打开此方法的事务
@ transactional (propagation=propagation.not _ supported ) readOnly=true为只读,不能更新或删除
@ transactional (propagation=propagation.required,readOnly=true )设置超时时间
@ transactional (传播=传播. required,timeout=30 )设置数据库的隔离级别
@ transactional (propagation=propagation.required,isolation=Isolation.DEFAULT )。