您的位置:首页 > 编程语言 > Java开发

Spring Hibernate 开启事务

2015-08-19 19:48 453 查看
仅仅记录一下配置文件

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" 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-4.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close" p:driverClass="com.mysql.jdbc.Driver"
		p:jdbcUrl="jdbc:mysql://hostname/test" p:user=""
		p:password="" p:maxPoolSize="200" p:minPoolSize="2"
		p:initialPoolSize="2" p:maxIdleTime="20"></bean>
		
		
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
		p:dataSource-ref="dataSource">

		<!-- annotatedClasses属性用于列出全部持久化类 -->
		<property name="annotatedClasses">
			<list>
				<!-- 以下用来列出Hibernate的持久化类 -->
				<value>com.songxu.entity.Log</value>
			</list>
		</property>
		<!-- 定义Hibernate的SessionFactory的属性 -->
		<property name="hibernateProperties">
			<!-- 指定数据库方言、是否自动建表、是否生成SQL语句等 -->
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true
				hibernate.format_sql=true
				#开启二级缓存
				hibernate.cache.use_second_level_cache=true
				#设置二级缓存的提供者
				hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
			</value>
		</property>

	</bean>

	<bean id="dao" class="com.songxu.entity.LogDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 开启事务 -->
	
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 配置事务增强处理Bean,指定事务管理器 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 用于配置详细的事务语义 -->
		<tx:attributes>
			<!-- 所有以'get'开头的方法是read-only的 -->
			<tx:method name="get*" read-only="true"  />
			<!-- 其他方法使用默认的事务设置 -->
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>

	<aop:config expose-proxy="true">
		<!-- 只对业务逻辑层实施事务 -->
		<aop:pointcut id="txPointcut" expression="execution(* com.songxu.entity.*.*(..))" />
		<!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
		<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
	</aop:config>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: