`
太阳神喻
  • 浏览: 105157 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

spring事务管理

阅读更多

有声明式事务管理和编程式事务管理,一般用声明式事务管理:结合dbcp和aop如:

<?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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<context:component-scan base-package="com.fsj" /><!--自动扫描-->
	<!-- 配置dbcp数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/s2shTest">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<!-- 事务管理器 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 事务管理策略 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
			<tx:method name="*" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面,设置对哪些类进行事务管理 -->
	<aop:config>
		<aop:pointcut id="ptc"
			expression="execution(* com.fsj.service.impl.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="ptc" />
	</aop:config>

</beans>

 

在dao层是采用JdbcTemplate:

package com.fsj.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.fsj.dao.UserDao;
import com.fsj.model.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

	private JdbcTemplate jdbcTemplate;

	@Resource
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

	
	@Override
	public void delete(int id) {
		System.out.println("delete"+id);
		this.jdbcTemplate.update(
		        "delete from userinfo where id = ?",
		        new Object[] {id});

	}

	@Override
	public User getById(int id) {
		System.out.println("getById"+id);
		User user = (User) this.jdbcTemplate.queryForObject(
			    "select * from userinfo where id = ?",
			    new Object[]{id},
			    new RowMapper() {
			        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			        	User user = new User();
			            user.setId(rs.getInt("id"));
			            user.setName(rs.getString("name"));
			            user.setPwd(rs.getString("pwd"));
			            return user;
			        }
			    });

		return user;
	}

	@Override
	public void save(User user) {
		System.out.println("save"+user.getName());
		this.jdbcTemplate.update("insert into userinfo (name, pwd) values (?, ?)", 
		        new Object[] {user.getName(), user.getPwd()});
	}

	@Override
	public void update(User user) {
		System.out.println("update"+user.getId());
		this.jdbcTemplate.update(
		        "update userinfo set pwd = ? where id = ?", 
		        new Object[] {user.getPwd(), user.getId()});
	}

}

 

 

 

 

 

 

 

 

 

配合hibernate和注解方式,进行声明式事务管理

<?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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<context:component-scan base-package="com.fsj" /><!-- 打开自动扫描 -->

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="file:src/hibernate.cfg.xml">
		</property>
	</bean>
	<!-- 基于hibernate的事务管理器 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:annotation-driven transaction-manager="txManager"/><!-- 注解式事务 -->
	
</beans>

 在需要事务管理的类上(一般是service层)使用注解:@Transactional进行事务管理

 

在dao层采用注入sessionFactory的时候初始化HibernateTemplate:

package com.fsj.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.fsj.dao.UserDao;
import com.fsj.model.Userinfo;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

	private HibernateTemplate hibernateTemplate;

	@Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

	
	@Override
	public void delete(int id) {
		hibernateTemplate.delete(hibernateTemplate.load(Userinfo.class, id));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Userinfo> getAll() {
		return (List<Userinfo>)hibernateTemplate.find("from Userinfo");
	}

	@Override
	public Userinfo getByid(int id) {
		return (Userinfo)hibernateTemplate.get(Userinfo.class, id);
	}

	@Override
	public void save(Userinfo user) {
		hibernateTemplate.persist(user);
	}

	@Override
	public void update(Userinfo user) {
		hibernateTemplate.merge(user);
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics