GlassFish3 配置连接池

1. 调用ConnectionPool 使用jndi 查找

在这个目录下有模板

glassfish-3.1.1\lib\install\templates\resources\jdbc

2. 这里我们以DB2和MySQL数据库为例

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

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<!DOCTYPE resources PUBLIC "-//Sun Microsystems Inc.//DTD Application Server 9.0 Domain//EN" "*<install directory>/lib/dtds/sun-resources_1_3.dtd*">

<!--
    Sun bundled DataDirect driver for Oracle Databases. (Available in SJSAS EE builds)
    JAR files for the driver are smdb2.jar, smbase.jar, smutil.jar

    To learn more on JDBC resources, pl. refer to JDBC resources chapter of admin guide.
    Administration Guide >> JDBC Resources  >> Configurations for Specific JDBC Drivers
    http://docs.sun.com/app/docs/doc/819-3658/6n5s5nklk?a=view

    For detailed explanation of attributes and elements of this resources xml
    pl. refer to sun-resources_1_3.dtd
-->

<resources>
    <jdbc-connection-pool
        name="db2_dd_pool"
        datasource-classname="com.sun.sql.jdbcx.db2.DB2DataSource"
        res-type="javax.sql.DataSource">
        <property name="user" value="DB_USER"/>
        <property name="password" value="DB_PASSWORD"/>
        <property name="serverName" value="DB_HOSTNAME"/>
        <property name="url" value="jdbc:sun:db2://DB_HOSTNAME:50000;databaseName=DATABASE_NAME"/>
        <property name="portNumber" value="50000"/>
        <property name="databaseName" value="DATABASE_NAME"/>
    </jdbc-connection-pool>

    <jdbc-resource
        enabled="true"
        jndi-name="jdbc/db2_dd_resource"
        object-type="user"
        pool-name="db2_dd_pool"/>
</resources>

MySql 连接池

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

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<!DOCTYPE resources PUBLIC "-//Sun Microsystems Inc.//DTD Application Server 9.0 Domain//EN" "*<install directory>/lib/dtds/sun-resources_1_3.dtd*">

<!--
    MySQL Type 4 Driver
    The JAR file for the MySQL driver is mysql-connector-java-version-bin-g.jar, 
    for example, mysql-connector-java-5.0.5-bin.jar.

    To learn more on JDBC resources, pl. refer to JDBC resources chapter of admin guide.
    Administration Guide >> JDBC Resources  >> Configurations for Specific JDBC Drivers
    http://docs.sun.com/app/docs/doc/819-3658/6n5s5nklk?a=view

    For detailed explanation of attributes and elements of this resources xml
    pl. refer to sun-resources_1_3.dtd

    ======================================================================================================
    |  res-type                             datasource-classname                                         |
    ======================================================================================================
    |  javax.sql.DataSource                 com.mysql.jdbc.jdbc2.optional.MysqlDataSource                |
    |  javax.sql.ConnectionPoolDataSource   com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource  |
    |  javax.sql.XADataSource               com.mysql.jdbc.jdbc2.optional.MysqlXADataSource              |
    ======================================================================================================
-->

<resources>
    <jdbc-connection-pool 
        name="mysql5-pool" 
        datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        res-type="javax.sql.DataSource">
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
        <property name="url" value="jdbc:mysql://localhost:3306/itmanager"/>
    </jdbc-connection-pool>

    <jdbc-resource 
        enabled="true" 
        jndi-name="jdbc/mysql52" 
        object-type="user" 
        pool-name="mysql5-pool"/>
</resources> 

3. 在GlassFish 工作域中注册配置信息domain.xml

glassfish-3.1.1\domains\domain1\config

<resources>
    <jdbc-resource pool-name="mysql5-pool" jndi-name="jdbc/mysql52"></jdbc-resource>
    <jdbc-resource pool-name="SamplePool" jndi-name="jdbc/sample"></jdbc-resource>
    <jdbc-resource pool-name="__TimerPool" jndi-name="jdbc/__TimerPool" object-type="system-admin"></jdbc-resource>
    <jdbc-resource pool-name="DerbyPool" jndi-name="jdbc/__default"></jdbc-resource>
    <jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" res-type="javax.sql.DataSource" name="mysql5-pool">
      <property name="user" value="root"></property>
      <property name="password" value="123456"></property>
      <property name="url" value="jdbc:mysql://localhost:3306/itmanager"></property>
    </jdbc-connection-pool>
    <jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.EmbeddedXADataSource" res-type="javax.sql.XADataSource" name="__TimerPool">
      <property name="databaseName" value="${com.sun.aas.instanceRoot}/lib/databases/ejbtimer"></property>
      <property name="connectionAttributes" value=";create=true"></property>
    </jdbc-connection-pool>
    <jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.ClientDataSource" is-isolation-level-guaranteed="false" res-type="javax.sql.DataSource" name="DerbyPool">
      <property name="PortNumber" value="1527"></property>
      <property name="Password" value="APP"></property>
      <property name="User" value="APP"></property>
      <property name="serverName" value="localhost"></property>
      <property name="DatabaseName" value="sun-appserv-samples"></property>
      <property name="connectionAttributes" value=";create=true"></property>
    </jdbc-connection-pool>
    <jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" name="SamplePool">
      <property name="DatabaseName" value="sample"></property>
      <property name="User" value="app"></property>
      <property name="Password" value="app"></property>
      <property name="URL" value="jdbc:derby://localhost:1527/sample"></property>
      <property name="PortNumber" value="1527"></property>
      <property name="serverName" value="localhost"></property>
    </jdbc-connection-pool>
    <jdbc-resource pool-name="mysql5-pool" description="" jndi-name="mysql5"></jdbc-resource>
    <jdbc-connection-pool driver-classname="" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" res-type="javax.sql.DataSource" description="" name="uml">
      <property name="user" value="root"></property>
      <property name="password" value="123456"></property>
      <property name="url" value="jdbc:mysql://localhost:3306/itmanager"></property>
    </jdbc-connection-pool>
    <jdbc-resource pool-name="uml" description="" jndi-name="qqq"></jdbc-resource>
  </resources>
 

启动glassfish 服务器OK

使用Context API查找DataSource 

public static DataSource getDataSource() throws NamingException {
        Context context = new InitialContext();
        return (DataSource)context.lookup("mysql5");
    }

就可以查找到数据源了。。其它Jboss 和websphere 都是类型的。。

因为本人对Jboss 很熟悉。。所以这个希望对大家有所帮助

猜你喜欢

转载自mianhuaman.iteye.com/blog/1439256