关于spring 事务处理

来源:互联网 发布:流程图制作软件 vision 编辑:程序博客网 时间:2024/06/16 13:05



applicationContext.xml     中代码是这样的 ,其中这是纯spring 管理事务


<?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:p="http://www.springframework.org/schema/p"
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.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
">
<bean id="tpfb" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.lucas.dao.ScaryDao</value>
</list>
</property>
<property name="target" ref="scaryDaoImpl"></property>
<property name="transactionManager" ref="tx"></property>
<property name="transactionAttributes">
<props>
<prop key="tranfer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="scaryDaoImpl" class="com.lucas.dao.impl.ScaryDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3305/structs_db</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>

</beans>

其中 实现类为:::


applicationContext.xml   



package com.lucas.dao.impl;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.interceptor.TransactionProxyFactoryBean;


import com.lucas.dao.ScaryDao;


public class ScaryDaoImpl implements ScaryDao {


JdbcTemplate jdbcTemplate;
TransactionProxyFactoryBean f;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}


@Override
public void tranfer(){
String sql1="update scary as s set s.banlace=s.banlace+1000 where s.id=1";
String sql2="update scary as s set s.banlace=s.banlace-1000 where s.id=2";
jdbcTemplate.update(sql1);
jdbcTemplate.update(sql2);
}
}



ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ScaryDao ud=(ScaryDao)ac.getBean("tpfb");
ud.tranfer();

其中ScaryDaoImpl 不能  extends HibernateTemplate


applicationContext.xml   
原创粉丝点击