網頁

2017/11/27

Spring JBoss AS 5.1 JNDI Oracle Datasource 連線設定

JBoss AS 5.1 連線Oracle資料庫的設定檔預設為[JBOSS_HOME]/server/default/deploy/oracle-ds.xml

下面為oracle-ds.xml的範例,JNDI名稱為mydatasource
連線到的資料庫位址為jdbc:oracle:thin:@10.1.1.213:1521:mydb
Oracle資料庫版本為12c。

<?xml version="1.0" encoding="UTF-8"?>

<datasources>

  <local-tx-datasource>

    <jndi-name>mydatasource</jndi-name>
    <!-- <use-java-context>false</use-java-context> -->
    <connection-url>jdbc:oracle:thin:@10.1.1.213:1521:mydb</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>matt</user-name>
    <password>12345</password>

    <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
    
    <metadata>
        <type-mapping>Oracle12c</type-mapping>
    </metadata>

  </local-tx-datasource>

</datasources>

Spring配置檔設定如下,JNDI name為mydatasource,又oralce-ds.xml<use-java-context>預設為true,此參數限制datasource只能經由JBoss VM來獲取,且JNDI name要加上前綴java:java:代表一個全域的命名空間(namespace)

<?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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/jee 
                        http://www.springframework.org/schema/jee/spring-jee.xsd">

  <!-- 原本的的寫法 -->
  <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:mydatasource" />
  </bean>
  <!-- 或可使用Spring jee tag -->
  <!--  <jee:jndi-lookup id="dataSource" jndi-name="java:mydatasource"/> -->
  
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
  </bean>


</beans>

如果覺得文章有幫助的話還幫忙點個Google廣告,感恩。


參考:

沒有留言:

張貼留言