1 supporting programmatic things and declarations in spring transaction management, usually using declarative things management, declarative things management is based on the implementation of the AOP mechanism is very convenient to use.
2 Spring the management of things that support a single database resource and the management of things that span multiple database resources JTA the global thing.
3 provides several things management classes in spring, often datasourcetransactionmanager,hibernatetransactionmanager and Jtatransactionmanager.
Datasourcetransactionmanager: Data source things manager for the JDBC framework and mybatis of things.
Hibernatetransactionmanager:hibernate things manager for hibernate things management.
Jtatransactionmanager: Distributed things management, delegating things management to Java EE Server Things manager.
4 Hibernatetransactionmanager in the form of annotations for the management of things:
DAO layer for the Log DAO interface plus implementation and user interface plus implementation,:
Package Com.dao;import Com.entity.slog;public interface Logdao {public void Save (Slog log);}
Package Com.dao;import Javax.annotation.resource;import Org.hibernate.session;import org.hibernate.SessionFactory; Import Org.springframework.stereotype.repository;import Com.entity.Slog; @Repository (value= "Logdao") public class Logdaoimpl implements Logdao {private sessionfactory sessionfactory;public sessionfactory getsessionfactory () {return Sessionfactory;} @Resourcepublic void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} @Overridepublic void Save (Slog log) {try {Session session=sessionfactory.getcurrentsession (); Session.save (log); System.out.println ("Log:ok");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Package Com.dao;import Com.entity.student;public interface Userdao {public void Save (Student Student);}
Package Com.dao;import Javax.annotation.resource;import Org.hibernate.session;import org.hibernate.SessionFactory; Import Org.springframework.stereotype.repository;import com.entity.Student; @Repository (value= "Userdao") public Class Userdaoimpl implements Userdao{private sessionfactory sessionfactory;public sessionfactory getSessionFactory () { return sessionfactory;} @Resourcepublic void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} public void Save (Student Student) {try {Session session=sessionfactory.getcurrentsession (); Session.save (Student); System.out.println ("Student:ok");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Service layer, the user's service interface plus implementation:
Package Com.service;import Com.entity.student;public interface UserService {public void Save (Student Student);}
Package Com.service;import Java.util.date;import Javax.annotation.resource;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.propagation;import Org.springframework.transaction.annotation.transactional;import Com.dao.logdao;import Com.dao.UserDao;import Com.entity.slog;import com.entity.Student; @Service (value= "UserService") public class Userserviceimpl implements Userservice{private Userdao userdao;private Logdao logdao;public Logdao Getlogdao () {return LogDao;} @Resourcepublic void Setlogdao (Logdao logdao) {This.logdao = Logdao;} Public Userdao Getuserdao () {return userdao;} @Resourcepublic void Setuserdao (Userdao userdao) {This.userdao = Userdao;} @Transactional (propagation=propagation.required) @Overridepublic void Save (Student Student) {userdao.save (Student); Slog sl=new Slog (); Sl.setlogdate (new Date ()); Logdao.save (SL);}}
Entity class:
Package Com.entity;import Java.util.date;import Javax.persistence.entity;import javax.persistence.GeneratedValue; Import javax.persistence.Id; @Entitypublic class Slog {@Id @generatedvalueprivate Integer logid;private Date logdate; Public Integer Getlogid () {return logid;} public void Setlogid (Integer logid) {this.logid = Logid;} Public Date Getlogdate () {return logdate;} public void Setlogdate (Date logdate) {this.logdate = Logdate;}}
Package Com.entity;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import javax.persistence.Id; @Entitypublic class Student {@Id @generatedvalueprivate Integer studentid;private String Studentname;public Integer Getstudentid () {return studentid;} public void Setstudentid (Integer studentid) {this.studentid = StudentID;} Public String Getstudentname () {return studentname;} public void Setstudentname (String studentname) {this.studentname = Studentname;}}
Applicationcontext.xml:
<?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:tx= "Http://www.springframework.org/schema/tx" 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.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd "><context:component-scan base-package=" com.* "/><context:annotation-config/ ><tx:annotation-driven transaction-manager= "Txmanager"/><beanclass= " Org.springframework.beans.factory.config.PropertyPlacEholderconfigurer "><property name=" Locations "><value>classpath:c3p0.properties</value>< /property></bean><bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >< Property Name= "Driverclass" value= "${driverclass}" ></property><property name= "Jdbcurl" value= "${ Jdbcurl} "></property><property name=" user "value=" ${user} "></property><property name=" Password "value=" ${password} "></property></bean><bean id=" sessionfactory "class=" Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "><property name=" DataSource " ref= "DataSource" ></property><property name= "annotatedclasses" ><list><value> com.entity.student</value><value>com.entity.slog</value></list></property>< Property Name= "Hibernateproperties" ><props><prop key= "Hibernate.dialect" > Org.hibernate.dialect.oracle10gdialect</prop><prOp key= "Hibernate.show_sql" >true</prop><prop key= "Hibernate.hbm2ddl.auto" >update</prop>< /props></property></bean><bean id= "Txmanager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "/></bean></beans>
Test class:
Package Com.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.entity.student;import Com.service.userservice;public class Test {public static void main (string[] args) {ApplicationContext ac=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); UserService uc= (UserService) Ac.getbean ("UserService"); Student s=new Student (); S.setstudentname ("hh"); Uc.save (s);}}
5 when you get the session in DAO, you need Sessionfactory.getcurrentsession () to use the same session in the context;
6 @Transactional, on which method to add things to, put on top of the class, add things to all methods.
7 @Transactionnal (propagation=propagation.required) You can set the properties of things:
propagation specifies the propagation characteristics of a thing, 7 propagation characteristics are as follows:
1 propagation.required If there is a thing in the current environment, then join the current thing, do not exist, then open a new thing. This is the default feature and is often used.
2 Propagation.supports If there is a thing in the current environment, then join the current thing and not exist, then run in a non-object way.
3 Propagation.requires_new If there is a thing in the current environment, let the current thing hang (suspend execution), and then open a new thing, when the new thing is done, the original thing will continue to execute. If the current environment does not exist, it is also the opening of a new thing.
4 propagation.not_supported If there is a thing in the current environment, let the current thing hang, oneself non-thing way to run, when oneself execution completes, the thing that precedes is carried on. If there are no things in the current environment, they are also directly non-thing-run.
5 Propagation.never must not be executed within the scope of the object, the result is executed within the scope of the object, and the container throws an exception. It will only work if it is not connected to something.
6 propagation.nested If there is a thing in the current environment that runs in this nested thing, create a new thing if nothing is present. Independent of the father, this thing has multiple rollback guarantee points, the rollback of their own things will not affect the external things. Works only with the Datasourcetransactionmanager thing manager.
7 Propagation.mandatory The current method must join the current object and throw an exception if there is no current object.
8 @Transactionnal (readonly=true) is set to read-only, only for queries, and will create read-only connection, which can improve query efficiency.
9 @Transactional (TIMEOUT=50) Things time-out, in seconds.
@Transactional () Specifies the isolation level of a thing by isolation.
One @Transactional () can be used only on public-modified methods, and non-public methods will invalidate things.
12 Configuring things through XML:
1 Configuring the Things Manager
2 Setting things manager properties
3 Entering a set of properties into the corresponding service layer through AOP
Applicationcontext.xml
<?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:tx= "Http://www.springframework.org/schema/tx" 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.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd "><context:component-scan base-package=" com.* "/> <context:annotation-config/ > <!--<tx:annotation-driven transaction-manager= "Txmanager"/>--><beanclass= " Org.springframework.beans.factory.coNfig. Propertyplaceholderconfigurer "><property name=" Locations "><value>classpath:c3p0.properties</ Value></property></bean><bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource "><property name=" Driverclass "value=" ${driverclass} "></property><property name=" JdbcUrl "value= "${jdbcurl}" ></property><property name= "user" value= "${user}" ></property><property name= " Password "value=" ${password} "></property></bean><bean id=" sessionfactory "class=" Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "><property name=" DataSource " ref= "DataSource" ></property><property name= "annotatedclasses" ><list><value> com.entity.student</value><value>com.entity.slog</value></list></property>< Property Name= "Hibernateproperties" ><props><prop key= "Hibernate.dialect" > Org.hibernate.dialect.oracle10gdialect&lT;/prop><prop key= "Hibernate.show_sql" >true</prop><prop key= "Hibernate.hbm2ddl.auto" >update </prop></props></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= "get*" read-only= "true"/><tx:method name= "save*" propagation= "REQUIRED"/ ></tx:attributes></tx:advice> <aop:config> <aop:pointcut id= "Fooserviceoperation" expression= "Execution (public * com.service). *.save (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "fooserviceoperation"/> </aop:config></beans >
Spring Things Management