Spring + Hibernate DAO 事务

在eclipse3.4+spring2.5+hibernate3.2 +SQL2005下调试的DAO事务

 

1 Spring配置文件,applicationContext-db.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="placeholderConfig"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:db.properties</value>
		</property>
	</bean>

	<bean id="local_dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${db.driverClassName}" />
		<property name="url" value="${db.url};databaseName=${db.name.local}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
	</bean>

	<bean id="local_sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="local_dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>edu/dlut/nihao/lvhl/domain/Person.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<!--transactionManager-->
	<bean id="local_transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="local_sessionFactory" />
		</property>
	</bean>

	<bean id="local_transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="local_transactionManager" />
		<property name="transactionAttributeSource" ref="txAttributeSource" />
	</bean>

	<bean id="txAttributeSource"
		class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
		<property name="properties">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="cancel*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="buildXml*">PROPAGATION_REQUIRED</prop>
				<prop key="publish*">PROPAGATION_REQUIRED</prop>
				<prop key="copy*">PROPAGATION_REQUIRED</prop>
				<prop key="list*">PROPAGATION_REQUIRED</prop>
				<prop key="find*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED</prop>
				<prop key="deal*">PROPAGATION_REQUIRED</prop>
				<prop key="search*">PROPAGATION_REQUIRED</prop>
				<prop key="is*">PROPAGATION_REQUIRED</prop>
				<prop key="has*">PROPAGATION_REQUIRED</prop>
				<prop key="checkPass*">PROPAGATION_REQUIRED</prop>
				<prop key="changePass*">PROPAGATION_REQUIRED</prop>
				<prop key="login*">PROPAGATION_REQUIRED</prop>
				<prop key="logout*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<value>*DAO</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>local_transactionInterceptor</value>
			</list>
		</property>
	</bean>

	<bean id="local_personDAO" class="edu.dlut.nihao.lvhl.domain.PersonDAO">
		<property name="sessionFactory">
			<ref bean="local_sessionFactory" />
		</property>
	</bean>
</beans>

 

2. 数据库表定义,数据库名称:Local,表名:Person,表结构:


 

USE [LOCAL]
GO
/****** 对象:  Table [dbo].[Person]    脚本日期: 09/19/2010 10:01:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Person](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](50) NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

 
 3 定义数据库连接的属性文件,db.properties

#Created by JInto - www.guh-software.de
#Wed Aug 25 16:36:01 CST 2010
db.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
db.password=1234
db.port=1433
db.url=jdbc\:sqlserver\://10.87.40.148
db.username=sa
db.name.local=Local
db.name.foreign=Foreign

 

4 domain对象,类Person

package edu.dlut.nihao.lvhl.domain;

public class Person {
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

5 Hibernate映射文件,Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="edu.dlut.nihao.lvhl.domain">

	<class name="edu.dlut.nihao.lvhl.domain.Person" table="Person"
		select-before-update="false" dynamic-update="false" dynamic-insert="false"
		optimistic-lock="version">

		<id name="id" column="Id" type="java.lang.Long" length="19">
			<generator class="identity"></generator>
		</id>

		<property name="name" type="java.lang.String" update="true"
			insert="true" access="property" column="Name" length="50" not-null="false" />
	</class>
</hibernate-mapping>

 6 DAO定义,分别定义接口  IPersonDAO.java和实现类  PersonDAO.java。

      IPersonDAO.java的源代码:

package edu.dlut.nihao.lvhl.domain;

public interface IPersonDAO {
	void insertPerson(Object person);
}

 

      PersonDAO.java的源代码,继承自HibernateDaoSupport :

package edu.dlut.nihao.lvhl.domain;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class PersonDAO extends HibernateDaoSupport implements IPersonDAO {

	public void insertPerson(Object person) {
		getHibernateTemplate().save(person);
		ExceptionUtil.exceptionOccur();
	}
}

 

   注意有一行代码,ExceptionUtil.exceptionOccur();
   由于对数据库操作PersonDAO.insertPerson(Object person) 纳入事务管理,执行测试代码,在插入一条记录后,产生一个运行时异常,所以插入操作回滚,察看数据库Person表,显示没有插入记录。

      注释掉该行后,运行测试程序,察看数据库,插入成功。

      ExceptionUtil用于产生异常

package edu.dlut.nihao.lvhl.domain;

public final class ExceptionUtil {
	public static void exceptionOccur() {
		int[] arr = new int[1];
		arr[1] = 0;
	}
}

 

7  测试代码,

package edu.dlut.nihao.lvhl.domain;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ContextFactory {
	static ApplicationContext ctx = null;
	static String PERSON_NAME = "nihao";

	public static Object findService(String beanId) {

		if (ctx == null) {
			ctx = new ClassPathXmlApplicationContext(
					new String[] { "applicationContext-db.xml" });
		}

		return ctx.getBean(beanId);
	}

	public static void main(String[] args) {

		IPersonDAO localDao = (IPersonDAO) ContextFactory
				.findService("local_personDAO");

		Person person = new Person();
		person.setName(PERSON_NAME);

		localDao.insertPerson(person);

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值