Jboss使用数据源

1、配置数据源文件
数据源必需以 **-ds.xml作为文件名,并放在server\default\deploy下,不同数据库配置不同,可参考\docs\examples\jca下的数据源配置

下面配置多个数据源例子:

<?xml version="1.0" encoding="GBK"?>

<!-- ===================================================================== -->
<!--                                                                       -->
<!--  JBoss Server Configuration                                           -->
<!--                                                                       -->
<!-- ===================================================================== -->

<!-- $Id: mssql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->

  <!-- ======================================================================-->
  <!-- New ConnectionManager setup for Microsoft SQL Server 2005  driver     -->
  <!-- Further information about the Microsoft JDBC Driver version 1.1      -->
  <!-- can be found here:                                                   -->
  <!-- http://msdn2.microsoft.com/en-us/library/aa496082.aspx               -->
  <!-- ===================================================================== -->

<datasources>
<!--网络电话本数据源 Mysql-->
  <local-tx-datasource>
    <jndi-name>jdbc/aurora_contact_web</jndi-name>
    <connection-url>jdbc:mysql://192.168.1.166/aurora_contact_web?autoReconnect=true&useUnicode=true&characterEncoding=utf-8</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>app</user-name>
    <password>app</password>
        <!-- sql to call when connection is created
        <new-connection-sql>some arbitrary sql</new-connection-sql>
        -->

        <!-- sql to call on an existing pooled connection when it is obtained from pool
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
        -->

      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
      <metadata>
         <type-mapping>mysql</type-mapping>
      </metadata>
  </local-tx-datasource>

  <!--Aurora 数据源 Postgres-->

     <local-tx-datasource>
    <jndi-name>jdbc/aurora</jndi-name>
    <connection-url>jdbc:postgresql://localhost/aurora?useUnicode=true&characterEncoding=utf-8"</connection-url>
    <driver-class>org.postgresql.Driver</driver-class>
    <user-name>admin</user-name>
    <password>admin</password>
        <!-- sql to call when connection is created
        <new-connection-sql>some arbitrary sql</new-connection-sql>
        -->

        <!-- sql to call on an existing pooled connection when it is obtained from pool
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
        -->

      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
      <metadata>
         <type-mapping>PostgreSQL</type-mapping>
      </metadata>
  </local-tx-datasource>

</datasources>

 


2、配置jboss-web.xml
该文件为jboss特有文件,Tomcat应用需在WEB-INF中加入该文件,其作用与context.xml类似 

 

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE jboss-web
    PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>
<!--配置jaas的jndi-->
<!--<security-domain>java:/jaas/DefaultLogin</security-domain>   -->
<!-- 配置jndi方式 -->
<resource-ref>
<!-- 取这个名字要和web.xml中对应起来 -->
<res-ref-name>jdbc/aurora_contact_web</res-ref-name>
<!-- 数据方式 -->
<res-type>javax.sql.DataSource</res-type>
<!-- 这个名字是和 aurora-ds.xml中的jndi-name对应着,不同的就是多加了一个java:/,这是个规范 -->
<jndi-name>java:/jdbc/aurora_contact_web</jndi-name>
</resource-ref>
</jboss-web>

 
3、web.xml

<resource-ref> 
<description> </description> 
<res-ref-name>jdbc/aurora_contact_web</res-ref-name><!--与jboss-web.xml对应--> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 

 
4、应用使用数据源

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/aurora_contact_web"/>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值