论坛首页 入门技术论坛

spring 事务笔记(一)

浏览 2655 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-20  
这里使用hibernate编程式事务管理.HibernateTransactionManager实现了PlatformTransactionManager.Spring小组推荐采用第一种方法(即使用 TransactionTemplate)。
这个接口是控制事务属性
import java.sql.Connection;
public interface TransactionDefinition {
    int getPropagationBehavior();
    int getIsolationLevel();
    int getTimeout();
    boolean isReadOnly();
}

写一个方法用来设置事务属性
TransactionDefinition.PROPAGATION_REQUIRED表示当前方法必须运行在一个事务中运行.如果一个现有的事务正在进行中,它将在该方法的运行期间被挂起.如果使用jtatansactionManager的话,需要访问transcaionManager.

private TransactionDefinition getDefinition(int isolationLevel) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);
        def.setIsolationLevel(isolationLevel);
        
        return def;
    }

TransactionDefinition.ISOLATION_READ_COMMITTED这里传进去事务隔离级别.
允许在并发事务中已经提交后读取.可防止脏读.但幻读和不可重复读仍可能发生
 public void insert() {
	        TransactionStatus status = transactionManager.getTransaction(
	            getDefinition(TransactionDefinition.ISOLATION_READ_COMMITTED));
	        try {
	        	            A ab = new A(new Integer(188),"c++");
	            A abc = new A(new Integer(199),"java");
	            testDao.insert(ab);
	            testDao.insert(abc);
	            transactionManager.commit(status);
	        } catch (Throwable t) {
	            transactionManager.rollback(status);
	        }
	    
	    }

以上是具体的实现的类
package test;

import hibernate.A;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class ProgrammaticManager {
	private ITestDao testDao;
	private PlatformTransactionManager transactionManager;
	public ProgrammaticManager() {
	}
	public ProgrammaticManager(ITestDao testDao) {
		this.testDao=testDao;
	}
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}
	
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	
	public ITestDao getTestDao() {
		return testDao;
	}
	
	public void setTestDao(ITestDao iTestDao) {
		this.testDao=iTestDao;
	}
	private TransactionDefinition getDefinition(int isolationLevel) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);
        def.setIsolationLevel(isolationLevel);
        
        return def;
    }
	 public void insert() {
	        TransactionStatus status = transactionManager.getTransaction(
	            getDefinition(TransactionDefinition.ISOLATION_READ_COMMITTED));
	        try {

	            A ab = new A(new Integer(188),"c++");
	            A abc = new A(new Integer(199),"java");
	            testDao.insert(ab);
	            testDao.insert(abc);
	            transactionManager.commit(status);
	        } catch (Throwable t) {
	            transactionManager.rollback(status);
	        }
	    
	    }

}

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<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/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="agsfd"></property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="mappingResources">
			<value>hibernate/A.hbm.xml</value>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>

	</bean>

	<!-- 
	-->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<bean id="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
	</bean>
	<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<bean id="testDao" class="test.TestDao">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate" />
		</property>
	</bean>
	<bean id="programmaticManager" class="test.ProgrammaticManager">
		<property name="testDao">
		<ref bean="testDao"/>
		</property>
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
	</bean>
	
</beans>

package test;

import hibernate.A;

public interface ITestDao {
    public void   insert(A a);
}

package test;


import hibernate.A;

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

public class TestDao extends HibernateDaoSupport implements ITestDao {

	public void insert(A a) {

		 getHibernateTemplate().save(a);
	}

}

ackage test;


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

public class Test {
	public static void main(String[] args) {
		ApplicationContext acx = new ClassPathXmlApplicationContext(
				"file:src/applicationContext.xml");

//		TransactionManager transaction = (TransactionManager) acx
//				.getBean("transactionManager");
	     ProgrammaticManager pm= (ProgrammaticManager) acx.getBean("programmaticManager");
		  pm.insert();
	}
}

数据表a 两个字段
更方便的类是transactiontemplate用法跟hibernatetemplate差不多
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics