简体   繁体   English

GlassFish 4.1中的连接池

[英]Connection Pooling in GlassFish 4.1

In an old web application I used a tomcat server. 在旧的Web应用程序中,我使用了tomcat服务器。 In order to configure the connection pool for the web application I modified the context.xml file of the application. 为了配置Web应用程序的连接池,我修改了该应用程序的context.xml文件。 I did not do anything to the server files. 我没有对服务器文件做任何事情。 This way I was able to have different pools for different applications on the same server. 这样,我就可以为同一台服务器上的不同应用程序使用不同的池。 I am now developing a new web application but this time using a glassfish 4.1 server. 我现在正在开发一个新的Web应用程序,但是这次使用的是glassfish 4.1服务器。 Based on the internet searches I performed it seems that I will have to modify the server files themselves in order to set up the server for connection pooling. 根据我执行的Internet搜索,似乎我必须自己修改服务器文件才能为连接池设置服务器。 Is there a way to modify a file within the web application only and have this file be read by the server, the same way that the context.xml file works for tomcat? 有没有一种方法可以只修改Web应用程序中的文件,并让服务器读取该文件,就像context.xml文件适用于tomcat一样? Thanks. 谢谢。

You don't have to modify any server files. 您无需修改​​任何服务器文件。 You can create the connection pool from the Admin console which you can access at http://localhost:4848 您可以从管理控制台创建连接池,可以从http://localhost:4848

Then navigate to Resources > JDBC> JDBC Connection Pools to create a new connection pool. 然后导航到资源> JDBC> JDBC连接池以创建新的连接池。

You can also test the connection using the Ping button. 您也可以使用Ping按钮测试连接。

To use the connection in the application you need to do a JNDI lookup as below 要在应用程序中使用连接,您需要执行以下JNDI查找

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("datasource jndi name");
Connection connection = ds.getConnection();

Update What you are looking for is application scoped resources. 更新您正在寻找的是应用程序范围内的资源。 For resources to be specific to your application you need to create WEB-INF/glassfish-resources.xml file which will create resources when war is deployed and remove them when undeployed. 为了使资源特定于您的应用程序,您需要创建WEB-INF / glassfish-resources.xml文件,该文件将在部署war时创建资源,而在取消部署时将其删除。

glassfish-resources.xml 与GlassFish resources.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource   Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-connection-pool name="java:app/appConnectionPool" res-type="javax.sql.ConnectionPoolDataSource"
   datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
    <property name="ServerName" value="localhost"/>
    <property name="Port" value="3306" />
    <property name="DatabaseName" value="demojpa" />
    <property name="User" value="demojpa" />
    <property name="Password" value="password"/>
    <property name="Url" value="jdbc:mysql://localhost:3306/demojpa"/>
    <!-- property name="driverClass" value="com.mysql.jdbc.Driver"/ not required -->
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="java:app/jdbc/appDataSource" object-type="user" 
pool-name="java:app/appConnectionPool"/>
</resources>

Lookup the datasource using "java:app/jdbc/appDataSource" jndi name. 使用“ java:app / jdbc / appDataSource” jndi名称查找数据源。 Have created a blog post for this at Glassfish application scoped resources Glassfish应用程序范围内的资源上为此创建了博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM