Spring entry Blog [13. Xml declaration transactions in Spring]

Source: Internet
Author: User

Additional knowledge points:

Propagation feature of transactions: In the current execution environment. If multiple methods are nested and called to each other, the features of the transaction will be transmitted from the first method to the second and third ....


[Java]
/* Transaction propagation. If there is a transaction in the current execution environment
* Spread in the communication environment. If no transaction exists, a transaction is created.
* The default value is required.
* The readOnly attribute specifies that the transaction can only be a select operation. Insert, update, etc.
* Actions cannot be executed. Because of readonly restrictions, and get the transation of readonly = true
* The execution efficiency is higher than that of the former.
* For more transaction configurations, see:
* Http://static.springsource.org/spring/docs/2.5.6/reference/transaction.html#transaction-strategies
* 9.5.6.1. @ Transactionalsettings
*/
@ Transactional (propagation = Propagation. REQUIRED, readOnly = true)
Public void save (User u ){
U. setName ("zhanglong ");
UserLog log = new UserLog ();
Log. setMsg ("useradded ");
UserDaoImpl. save (u );
UserLogDaoImpl. save (log );
}
The following is a case study of xml configuration transactions:

1. A simple method for writing business logic operations:

[Java]
@ Component ("userService ")
Public class UserServiceImpl implements UserService {

@ Resource
Private UserDao userDaoImpl;
@ Resource
Private UserLogDao userLogDaoImpl;
 
Public UserLogDao getUserLogDaoImpl (){
Return userLogDaoImpl;
}
Public void setUserLogDaoImpl (UserLogDao userLogDaoImpl ){
This. userLogDaoImpl = userLogDaoImpl;
}
Public UserDao getUserDaoImpl (){
Return userDaoImpl;
}
Public void setUserDaoImpl (UserDao userDaoImpl ){
This. userDaoImpl = userDaoImpl;
}

Public void save (User u ){
U. setName ("zhanglong ");
UserLog log = new UserLog ();
Log. setMsg ("user added ");
UserDaoImpl. save (u );
UserLogDaoImpl. save (log );
}
}
1. Write the XML configuration file and add the transaction:

The following files mainly include the configuration of the database and the previous section of the specified transaction. We mainly look at the configuration of transactions in xml

1. Configure the Transaction Manager transactionManager

2. Configure the transaction management method tx: advice

3. Configure AOP to weave logical transactions into corresponding methods


[Html]
<? 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-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
<Context: annotation-config/>
<! -- Configure the container resource scan package -->
<Context: component-scan base-package = "com. spring"/>
<! -- Write the front class to the container -->
<Bean id = "logInterceptor" class = "com. spring. aop. LogInterceptor"/>
 
<! --
Configure the data source <bean id = "myDataSource"
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/sms"/> <property name = "username"
Value = "root"/> <property name = "password" value = "root"/> </bean>
-->
 
<! -- Placeholder -->
<Bean
Class = "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer">
<Property name = "locations">
<Value> classpath: jdbc. properties </value>
</Property>
</Bean>
<! -- Configure dataSource -->
<Bean id = "dataSource" destroy-method = "close"
Class = "org. apache. commons. dbcp. BasicDataSource">
<Property name = "driverClassName" value = "$ {jdbc. driverClassName}"/>
<Property name = "url" value = "$ {jdbc. url}"/>
<Property name = "username" value = "$ {jdbc. username}"/>
<Property name = "password" value = "$ {jdbc. password}"/>
</Bean>
 
<! -- Inject the configured dataSource into SessionFactory -->
<Bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "mappingResources">
<List>
<Value> com/spring/model/user. hbm. xml </value>
<Value> com/spring/model/userlog. hbm. xml </value>
</List>
</Property>
<Property name = "hibernateProperties">
<Value>
Hibernate. dialect = org. hibernate. dialect. MySQLDialect
Hibernate. show_ SQL = true
Hibernate. hbm2ddl. auto = create
</Value>
</Property>
</Bean>
 
<! -- --------------------------- The above is the configuration of the data source and sessionFactory ------------------------- -->
 
<! -- Declarative transaction management: the transaction needs a data source, which is obtained from sessionFactory.
This is an application of AOP -->
<Bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"/>
</Bean>
 
<! -- Configure the method for managing transactions -->
<Tx: advice transaction-manager = "transactionManager" id = "txManager">
<Tx: attributes>
<Tx: method name = "save" read-only = "true"/>
</Tx: attributes>
</Tx: advice>
 
<! -- Configure aop to set the cut and weaving point logic -->
<Aop: config>
<Aop: pointcut id = "entryPointMethod" expression = "execution (public * com. spring. service... * (...)"/>
<Aop: advisor
Advice-ref = "txManager"
Pointcut-ref = "entryPointMethod"
/>
</Aop: config>
</Beans>
Author: zhang6622056
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.