`

GlassFish3 配置连接池

    博客分类:
  • EJB
 
阅读更多

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 很熟悉。。所以这个希望对大家有所帮助

 

 

 

 

 

 

 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

    在GlassFish中配置数据源连接池

    2. 配置连接池 在管理控制台中,选择“连接池”&gt;“新建”,然后按照提示进行配置,包括指定数据库连接的属性,如数据库类型、主机名、端口号、用户名和密码等。 3. 配置 JDBC 数据源 在配置连接池后,需要配置一...

    glassfish建立连接池

    下面,我们将介绍如何在 GlassFish 管理员界面中配置连接池,特别是使用 Oracle 数据库为例。 连接池的概念 ------------- 连接池是一种提高应用程序性能和可靠性的技术,它可以将多个数据库连接组合成一个池,...

    eclipse与glassfish3.1.1使用配置-连接池配置-JNDI配置[归纳].pdf

    要配置连接池,需要遵循以下步骤: 1. 创建连接池 2. 配置连接池参数 3. 测试连接池 四、JNDI 配置 JNDI(Java Naming and Directory Interface)是 Java EE 中的一个命名和目录接口,用于查找和访问 Java EE 组件...

    连接池的配置

    在配置连接池时,不同的连接池实现(如Apache DBCP、C3P0、HikariCP、Tomcat JDBC Pool等)会有各自的属性和配置方式。例如,HikariCP以其高性能而著称,其配置项包括`maximumPoolSize`、`idleTimeout`、`connection...

    glassfish配置jndi(DB2数据库)

    **Step6:** 配置连接池参数,如最大连接数、最小连接数、超时时间等,以满足你的应用需求。 **Step7:** 完成数据源配置后,需要创建一个JNDI别名,使得应用可以通过JNDI查找这个数据源。回到“JDBC Resources”...

    GlassFish服务器集群搭建.pdf

    3. **配置连接池参数**:填写相关配置信息,包括数据库URL、用户名、密码、最小和最大连接数等,然后保存设置。 4. **创建JDBC Resources (JNDI)**:定义一个JNDI名称,与之前创建的连接池关联,这样应用可以通过...

    Glassfish创建数据源

    - 使用连接池来优化数据库连接管理,如C3P0、HikariCP或Apache DBCP,它们能有效管理和复用数据库连接,减少资源消耗。 - 定期检查和更新数据源配置,以适应数据库的升级或安全策略变更。 - 在生产环境中,确保...

    GlassFish安装向导

    在 GlassFish 中,需要配置 JDBC 连接池。配置完成后,需要创建受管节点和实例。创建受管节点可以使用以下命令:`./asadmin create-instance --host h3cpdm20-ts --port 4848 -nodeagent h3cpdm20-tsNode`。创建完成...

    glassfish网络开发服务器

    7. **性能优化**:内置了JVM调优工具,可以分析和优化服务器性能,同时支持连接池和缓存管理,以提高响应速度。 8. **社区支持**:GlassFish拥有活跃的开发者社区,不断推动其发展和更新,提供各种插件、教程和问题...

    GlassFish 2.1.1QuickStartGuide

    - **连接池配置:** 调整数据库连接池的最大连接数、空闲时间等参数。 - **缓存机制:** 实现应用层面的缓存机制来减少数据库访问频率。 **2. 高可用性与集群:** - **集群部署:** 通过集群实现负载均衡和故障...

    glassfish官方文档

    2. **域管理**:GlassFish使用域的概念来组织多个服务器实例,域配置包括了服务器设置、数据源、连接池、安全管理器等。文档会详细介绍如何创建、配置和管理这些域。 3. **命令行工具**:GlassFish提供了一套强大的...

    在Glassfish上部署web应用

    在Glassfish管理控制台的“Resources”-&gt;“JDBC”部分,你可以创建JDBC资源和连接池。这通常涉及设置数据源名称、JDBC驱动类、URL、用户名和密码等属性。配置完成后,应用程序可以通过JNDI查找来访问这些数据源。 ...

    glassfish api

    内容可能包括内存配置、线程池调整、数据库连接池优化、日志和监控等方面。性能调优是确保应用高效运行的关键,它涉及到资源分配、并发控制、缓存策略等多个方面。 5. **使用手册** Glassfish API的使用手册是...

    GlassFish部署项目说明.doc

    例如,你可以通过管理控制台创建和管理数据源、连接池、邮件会话,以及监控应用程序性能。 - 数据源配置:为应用程序提供数据库连接,通过管理控制台可以添加、删除和配置JDBC数据源。 - 集群部署:在多个GlassFish...

    GlassFish 解析

    在GlassFish中配置数据源,需要在管理控制台创建JDBC资源和连接池,指定数据库驱动、URL、用户名和密码等信息,以使应用能够连接到数据库。 六、GlassFish 设置及其安装常见问题 在使用过程中,可能会遇到各种问题...

    glassfish-4.1.2.zip

    4. 数据源配置:如果应用需要数据库连接,需创建并配置数据源,如JDBC资源和连接池。 三、应用部署 1. 模块化部署:支持WAR、EAR、JAR等多种部署方式,满足不同类型的应用需求。 2. 热部署:无需重启服务器,即可...

    一般WEB项目(rh5linux+java+mysql+glassfishV3)部署文档知识.pdf

    5. 配置数据源:在Glassfish中,配置与MySQL数据库的连接,如JDBC资源和连接池。 6. 测试应用:访问Web应用的URL,确保部署成功且能正常运行。 通过以上步骤,你将能够在RED HAT 5.5 Linux系统上成功部署一个基于...

    Sun glassfish Performance Tuning Guide

    - 选择正确的数据库连接池实现,如HikariCP或C3P0,根据应用需求设置合理的连接池大小。 - 监控连接池的使用情况,避免因连接泄露或超时造成的资源浪费。 #### 5. **缓存机制的应用** - 实现有效的缓存策略,...

    SUN GlassFish拥抱Spring.rar

    5. **性能优化**:结合GlassFish和Spring,讨论如何优化应用性能,例如缓存策略、连接池配置、线程池管理等。 6. **故障排查与监控**:介绍在GlassFish中监控Spring应用的方法,包括日志配置、JMX管理、性能指标...

Global site tag (gtag.js) - Google Analytics