JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Application Framework » Spring  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 請教一下 Spring整合Hibernate和JDBC的問題
legendker





發文: 41
積分: 0
於 2011-10-19 14:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因為需求的關係 須要同時整合Hibernate和JDBC 畢竟Hibernate方便歸方便 但是對寫JDBC習慣的我 JDBC自由度更高 先把我整合的application-context.xml PO上來

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
  default-lazy-init="true">
 
  <description>Spring公共配置 </description>
 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
      <list>
        <!-- 標準配置 -->
        <value>classpath*:/application.properties</value>
      </list>
    </property>
  </bean>
 
  <!-- 使用annotation 自動註冊bean -->
  <context:component-scan base-package="com.onlinedoc" />
            
  <!-- 此用DBCP連接池 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- Connection Info -->
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
 
    <!-- Connection Pooling Info -->
    <property name="initialSize" value="5" />
    <property name="maxActive" value="100" />
    <property name="maxIdle" value="30" />
    <property name="maxWait" value="500" />
    <property name="poolPreparedStatements" value="false" />
    <property name="defaultAutoCommit" value="false" />
  </bean>
  
  <!-- Hibernate配置 -->
  <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" />
    <property name="namingStrategy">
      <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
        </prop>
        <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>      
      </props>
    </property>
    <property name="packagesToScan" value="com.onlinedoc.*" /> 
  </bean>
  
  <!-- 事務管理器配置 -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  
  <!-- 使用annotation @Transaction自動注入 -->
  <tx:annotation-driven transaction-manager="transactionManager"/>
 
    <!-- Transaction manager for a single JDBC DataSource JoelADD 2010.02.12 -->
    <bean id="transactionManager_jdbc" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
     
    <!-- Transaction template for Managers -->
    <bean id="txProxyTemplate_jdbc" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager_jdbc" />
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="add*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
</beans>


測試出來的結果 若是DAO bean繼承txProxyTemplate-jdbc 也就是走JDBC這條
詢問資料庫沒有問題
但是如果是用Hibernate的話 就會直接跳500 error, console下沒有任何錯誤訊息

小弟的問題是 如果單用JDBC 那麼session是不是要自己管理?
還有TransactionProxyFactoryBean需不需要再設定一份Hibernate的
將 <property name="transactionManager" ref="transactionManager_jdbc" />
改為
<property name="transactionManager" ref="transactionManager" />
在這邊的整合上 出了點問題 麻煩各位高手指點一下 謝謝!!


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1369 請教一下 Spring整合Hibernate和JDBC的問題 legendker 4974 2011-10-19 14:08
1236 Re:請教一下 Spring整合Hibernate和JDBC的問題 kentyeh 154 2011-10-19 15:15
1220 Re:請教一下 Spring整合Hibernate和JDBC的問題 legendker 857 2011-10-19 15:42
1329 Re:請教一下 Spring整合Hibernate和JDBC的問題 kentyeh 111 2011-10-19 16:16
» JWorld@TW »  Java Application Framework » Spring

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8