補充知識點:
事務的傳播特性:在當前的執行環境中。如果有多個方法嵌套互相調用的話,那麼事務的特性必將從第一個方法傳播到第二個、第三個。。。。
[java]
/*事務的傳播性。如果當前執行環境中有事務,那麼則會一直在
* 傳播環境中傳播下去。如果沒有事務那麼則會創建一個事務
* 默認就是required
* 而readOnly屬性則是規定該事務隻能是一些select操作。而insert,update等
* 動作是不能執行的。因為readonly的限制,而且拿到readonly=true的transation
* 的話它的執行效率是比前者高的
* 還有更多事務配置請在:
*https://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);
}
下面看一個xml配置事務的案例:
1、 編寫業務邏輯操作的簡單方法:
[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、 編寫XML配置文件,將事務加上去:
下面的文件主要包括配置數據庫和指定事務前一段不用看。我們主要看事務在xml中的配置
1、 配置事務管理器transactionManager
2、 配置事務要管理的方法 tx:advice
3、 配置AOP將邏輯事務織入到相應的方法上去
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-2.5.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx-2.5.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<!– 配置容器資源掃描的包 –>
<context:component-scan base-package="com.spring" />
<!– 將前面類寫入容器 –>
<bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />
<!–
配置數據源 <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>
<!– 配置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>
<!– 將配置好的dataSource註入到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>
<!– —————————以上是數據源和sessionFactory的配置 —————————– –>
<!– 聲明式事務管理,事務需要數據源,從sessionFactory中拿到
這是一個AOP的應用 –>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!– 配置事務要管理的方法 –>
<tx:advice transaction-manager="transactionManager" id="txManager">
<tx:attributes>
<tx:method name="save" read-only="true"/>
</tx:attributes>
</tx:advice>
<!– 配置aop設置切面和織入點邏輯 –>
<aop:config>
<aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>
<aop:advisor
advice-ref="txManager"
pointcut-ref="entryPointMethod"
/>
</aop:config>
</beans>
作者:zhang6622056