WebLogic-JNDI数据源+WEB项目使用

一,配置数据库连接信息文件

新增jdbcxxx_domian\config目录下Base-WebData-jdbc.xml。

<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="http://www.bea.com/ns/weblogic/jdbc-data-source" xmlns:sec="http://www.bea.com/ns/weblogic/90/security" xmlns:wls="http://www.bea.com/ns/weblogic/90/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/jdbc-data-source http://www.bea.com/ns/weblogic/jdbc-data-source/1.0/jdbc-data-source.xsd">
  <name>WebData</name>
  <internal-properties>
    <property>
      <name>LegacyType</name>
      <value>1</value>
    </property>
    <property>
      <name>TestConnectionsOnRelease</name>
      <value>true</value>
    </property>
  </internal-properties>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@localhost:1521:orcl</url>
    <driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>用户</value>
      </property>
    </properties>
    <password-encrypted>密码</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>3</initial-capacity>
    <max-capacity>100</max-capacity>
    <capacity-increment>10</capacity-increment>
    <shrink-frequency-seconds>900</shrink-frequency-seconds>
    <test-connections-on-reserve>true</test-connections-on-reserve>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
  </jdbc-connection-pool-params>
</jdbc-data-source>

其中WebData作为键值,为JNDI数据源提供连接信息。

二,配置JNDI jdbc/webdata数据源文件

新增jdbcxxx_domian\config目录下DS-WebDataSource-0001-jdbc.xml。

<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="http://www.bea.com/ns/weblogic/jdbc-data-source" xmlns:sec="http://www.bea.com/ns/weblogic/90/security" xmlns:wls="http://www.bea.com/ns/weblogic/90/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/jdbc-data-source http://www.bea.com/ns/weblogic/jdbc-data-source/1.0/jdbc-data-source.xsd">
  <!--此处可随意填写,无处使用此值-->
  <name>WebDataSource1</name>
  <internal-properties>
    <property>
      <name>LegacyType</name>
      <value>4</value>
    </property>
    <property>
      <name>LegacyPoolName</name>
	  <!--此处配置出数据库连接信息文件Base-WebData-jdbc.xml中name:WebData-->
      <value>WebData</value>
    </property>
  </internal-properties>
  <jdbc-data-source-params>
    <jndi-name>jdbc/webdata</jndi-name>
  </jdbc-data-source-params>
</jdbc-data-source>

三,配置config.xml,导入数据源文件Base-WebData-jdbc.xml、DS-WebDataSource-0001-jdbc.xml

修改WebLogic配置文件_domain\config\config.xml,在在<admin-server-name>AdminServer</admin-server-name> 行之后添加:

  <admin-server-name>AdminServer</admin-server-name>
  <jdbc-system-resource>
    <name>Base-WebData</name>
    <target>AdminServer</target>
    <deployment-order>1</deployment-order>
    <descriptor-file-name>jdbc/Base-WebData-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>DS-WebDataSource</name>
    <target>AdminServer</target>
    <deployment-order>4</deployment-order>
    <descriptor-file-name>jdbc/DS-WebDataSource-0001-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
</domain>

四,在WEB应用XXX_Web中,注册ENC、JNDI数据源whu/jdbc/WebData。

XXX_Web\WEB-INF\weblogic.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN" "http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">
<weblogic-web-app>
    <reference-descriptor> 
      <resource-description> 
		<!--nstc/jdbc/WebData 即为J2EE环境中ENC名,在程序中可通过 "java/comp/env/"+此名 访问底层JNDI-->
        <res-ref-name>whu/jdbc/WebData</res-ref-name> 
        <jndi-name>jdbc/webdata</jndi-name> 
      </resource-description> 
    </reference-descriptor>    
    <container-descriptor> 
        <prefer-web-inf-classes>true</prefer-web-inf-classes> 
    </container-descriptor> 
</weblogic-web-app>

XXX_Web\WEB-INF\web.xml配置引入ENC-JNDI数据源whu/jdbc/WebData
	<!-- ===============================引入ENC-JNDI数据源whu/jdbc/WebData ==================================== -->
	<resource-ref>
		<res-ref-name>
// Obtain the application component's ENC   
Context iniCtx = new InitialContext();   
Context compEnv = (Context) iniCtx.lookup("whu/jdbc/WebData");   

</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref></web-app>

五,在应用XXX_Web的程序中使用ENC-JNDI数据源



在JAVA程序中、JSP中使用。 (不推荐JSP使用)
// Obtain the application component's ENC   
Context iniCtx = new InitialContext();   
Context compEnv = (Context) iniCtx.lookup("whu/jdbc/WebData");   
在spring框架中使用
<?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: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-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- ======================================================================================= -->
    <!--                                                                                         -->
    <!--                                          数据源               		                             -->
    <!--                                                                                         -->
    <!-- ======================================================================================= -->

    <bean id="framework.dataSource"
        class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:comp/env/whu/jdbc/WebData</value>
        </property>
    </bean>
</beans>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值