日期:2014-05-16  浏览次数:20350 次

jdbc和ibatis与spring中sessionFactory等价的类(帮助处理事务)
其它数据访问技术的等价类

理解了 Spring JDBC 的数据连接泄漏问题,其中的道理可以平滑地推广到其它框架中去。Spring 为每个数据访问技术框架都提供了一个获取事务上下文绑定的数据连接(或其衍生品)的工具类和数据源(或其衍生品)的代理类。

DataSourceUtils 的等价类

下表列出了不同数据访问技术对应 DataSourceUtils 的等价类:


表 3. 不同数据访问框架 DataSourceUtils 的等价类
数据访问技术框架	连接 ( 或衍生品 ) 获取工具类
Spring JDBC	 org.springframework.jdbc.datasource.DataSourceUtils
Hibernate	 org.springframework.orm.hibernate3.SessionFactoryUtils
iBatis	 org.springframework.jdbc.datasource.DataSourceUtils
JPA	 org.springframework.orm.jpa.EntityManagerFactoryUtils
JDO	 org.springframework.orm.jdo.PersistenceManagerFactoryUtils

TransactionAwareDataSourceProxy 的等价类

下表列出了不同数据访问技术框架下 TransactionAwareDataSourceProxy 的等价类:


表 4. 不同数据访问框架 TransactionAwareDataSourceProxy 的等价类
数据访问技术框架	连接 ( 或衍生品 ) 获取工具类
Spring JDBC	 org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
Hibernate	 org.springframework.orm.hibernate3.LocalSessionFactoryBean
iBatis	 org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
JPA	 无
JDO	 org.springframework.orm.jdo.
TransactionAwarePersistenceManagerFactoryProxy

-----------------------------------------------分割线----------------------------------------------------


hibernate中sessionFactory配置如下(具备事务管理功能):
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="mysqlDataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
			</props>
		</property>
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath*:/com/xuyi/modal</value>
			</list>
		</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory">
		<ref bean="sessionFactory" />
	</property>
</bean>


jdbcTemplate和ibatis中datasource代理配置如下(具备事务管理功能):
	
<bean id="dataSourceProxy"
	class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
	<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="transactionManager"  
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    <property name="dataSource" ref="dataSourceProxy" />  
</bean>