Spring.x+Hibernate__java

來源:互聯網
上載者:User
 二、Spring.x+Hibernate+Atomikos整合

    Spring.x+Hibernate怎麼整合此處省略不說,主要講解Spring.x+Atomikos的整合,操作步驟如下:

    1、Atomikos,需要以下這些包

    atomikos-util-1.0.jar

    cglib-nodep-2.2.2.jar

    transactions-3.7.0.jar

    transactions-api-3.7.0.jar

    transactions-jdbc-3.7.0.jar

    transactions-jta-3.7.0.jar

    2、配置

    在applicationContext.xml檔案當中作如下配置

    <?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-3.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.5.xsd

    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"

    default-autowire="byName">

    <!-- 使Spring關注Annotation -->

    <context:annotation-config />

    <!-- 讀取參數設定檔資訊 -->

    <!-- <context:property-placeholder location="classpath*:jta.properties"/> -->

    <bean id="BaseService" class="com.test.service.impl.BaseServiceImpl" />

    <!-- 讓Spring通過自動掃描來查詢和管理Bean -->

    <context:component-scan base-package="com.test">

    <context:include-filter type="regex" expression=".dao.impl.*"/>

    <context:include-filter type="regex" expression=".serviec.impl.*"/>

    <context:include-filter type="regex" expression=".entity.*"/>

    </context:component-scan>

    <!-- 資料庫tms -->

    <bean class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"

    id="tmsDataSource">

    <!-- Set unique name for this DataSource -->

    <property name="uniqueResourceName"><value>mysql/tms</value></property>

    <!-- Set XADatasource class name-->

    <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />

    <property name="xaProperties">

    <props>

    <prop key="user">root</prop>

    <prop key="password">root</prop>

    <prop key="url">jdbc:mysql://localhost:3306/tms</prop>

    </props>

    </property>

    <!-- set properties for datasource connection pool -->

    <property name="poolSize" value="3" />

    <!-- 管理 Connection 被佔用的時間 -->

    <!-- 如果不設定這個值,Atomikos使用預設的300秒(即5分鐘),那麼在處理大批量資料讀取的時候,一旦超過5分鐘,就會拋出類似 Resultset is close 的錯誤 -->

    <property name="reapTimeout"><value>20000</value></property>

    </bean>

    <!-- 資料庫his -->

    <bean class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"

    id="hisDataSource">

    <property name="uniqueResourceName"><value>mysql/his</value></property>

    <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />

    <property name="xaProperties">

    <props>

    <prop key="user">root</prop>

    <prop key="password">root</prop>

    <prop key="url">jdbc:mysql://localhost:3306/his</prop>

    </props>

    </property>

    <!-- 串連池裡面串連的個數 -->

    <property name="poolSize" value="3" />

    <!-- 管理 Connection 被佔用的時間 -->

    <!-- 如果不設定這個值,Atomikos使用預設的300秒(即5分鐘),那麼在處理大批量資料讀取的時候,一旦超過5分鐘,就會拋出類似 Resultset is close 的錯誤 -->

    <property name="reapTimeout"><value>20000</value></property>

    </bean>

    <!-- sessionFactory的配置 -->

    <!-- 將hibernate資料來源注入sessionFactory -->

    <bean id="tmsSessionFactory"

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="tmsDataSource" />

    <property name="packagesToScan">

    <list>

    <value>com.test.entity</value>

    </list>

    </property>

    <property name="hibernateProperties">

    <props>

    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

    <prop key="hibernate.show_sql">true</prop>

    <!-- 謹慎(推薦禁止) 固定為update -->

    <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->

    <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>

    </props>

    </property>

    </bean>

    <!-- 將hibernate資料來源注入sessionFactory -->

    <bean id="hisSessionFactory"

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="hisDataSource" />

    <property name="packagesToScan">

    <list>

    <value>com.test.entity</value>

    </list>

    </property>

    <property name="hibernateProperties">

    <props>

    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

    <prop key="hibernate.show_sql">true</prop>

    <!-- 謹慎(推薦禁止) 固定為update -->

    <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->

    <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>

    </props>

    </property>

    </bean>

    <!-- atomikos交易管理員 -->

    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"

    init-method="init" destroy-method="close">

    <description>UserTransactionManager</description>

    <property name="forceShutdown">

    <value>true</value>

    </property>

    </bean>

    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">

    <property name="transactionTimeout" value="300" />

    </bean>

    <!-- spring 交易管理員 -->

    <bean id="transactionManager"

    class="org.springframework.transaction.jta.JtaTransactionManager">

    <property name="transactionManager" ref="atomikosTransactionManager"/>

    <property name="userTransaction" ref="atomikosUserTransaction" />

    <property name="allowCustomIsolationLevels" value="true"/>

    </bean>

    <!-- 使用annotation定義事務,對於要加入事物的類,只需對該類加 @Transactional  -->

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- hibernate Dao層模板 -->

    <bean id="tmsHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

    <property name="sessionFactory" ref="tmsSessionFactory"></property>

    </bean>

    <bean id="hisHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

    <property name="sessionFactory" ref="hisSessionFactory"></property>

    </bean>

    </beans>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.