在glassfish中建立串連池

來源:互聯網
上載者:User

原文:http://purebit.net/archiver/showtopic-50.aspx

 

Smartwater - 2007-6-28 9:10:00

creating jdbc connection pool & resource in GlassFish

JDBC Connection Pool & Resource can be created in multiple ways.

  • Admin Console
  • CLI
  • Using sun-resources.xml templates
  • Creating Connection Pool & Resource programmatically

1)
Admin Console  Admin console provides user intuitive interface to
create a connection pool & resource for database access.
a) Copy the jdbc driver that will be used for database connectivity, to GLASSFISH_INSTALL/domains/<domainname>/lib/ext or
add the jdbc-driver path to "classpath-prefix" and restart application server.
b) Using Admin console, Common Tasks > create jdbc connection pool

c) Provide a name for the connection pool, select the resource type, select the database vendor and press Next

d) Console will list the the default pool settings and also the properties of datasource class provided by the vendor.

e) Fill the values for datasource properties, eg: servername, user, password, URL, databasename etc., and press Finish
f) Pool will be created

g) To check whether the values provided for datasource for database connectivity are valid and works fine, press Ping.
Ping will use these properties and try to establish a connection.

This connection pool need to be associated with a jdbc-resource.
h) To create a jdbc-resource that will be used by the application :
Resources > Jdbc Resources

i) Press New
j) provide the jndi name for the resource and the pool with which the resource need to be associated.

k) Press OK, jdbc-resource will be created.

2) Using CLIjdbc-connection-pool can be created as

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin
create-jdbc-connection-pool --datasourceclassname
oracle.jdbc.pool.OracleDataSource --restype javax.sql.DataSource
--property
user=dbuser:password=dbpassword:url="jdbc/:oracle/:thin/:@localhost/:1521/:ORCL"
oracle-pool
            Command create-jdbc-connection-pool executed successfully.

jdbc-resource can be created as

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin create-jdbc-resource --connectionpoolid oracle-pool jdbc/oracle-resource
            Command create-jdbc-resource executed successfully.

To test whether the connection settings are correct, ping connection pool

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin ping-connection-pool oracle-pool
            Command ping-connection-pool executed successfully.

Properties of connection pool can be read/changed as follows.

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin list *oracle-pool*
            domain.resources.jdbc-connection-pool.oracle-pool

List the properties of connection pool

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin get domain.resources.jdbc-connection-pool.oracle-pool.*
            domain.resources.jdbc-connection-pool.oracle-pool.allow-non-component-callers = false
            domain.resources.jdbc-connection-pool.oracle-pool.associate-with-thread = false
            domain.resources.jdbc-connection-pool.oracle-pool.connection-creation-retry-attempts = 0
            domain.resources.jdbc-connection-pool.oracle-pool.connection-creation-retry-interval-in-seconds = 10
            domain.resources.jdbc-connection-pool.oracle-pool.connection-leak-reclaim = false
            domain.resources.jdbc-connection-pool.oracle-pool.connection-leak-timeout-in-seconds = 0
            domain.resources.jdbc-connection-pool.oracle-pool.connection-validation-method = auto-commit
            domain.resources.jdbc-connection-pool.oracle-pool.datasource-classname = oracle.jdbc.pool.OracleDataSource
            domain.resources.jdbc-connection-pool.oracle-pool.description =
            domain.resources.jdbc-connection-pool.oracle-pool.fail-all-connections = false
            domain.resources.jdbc-connection-pool.oracle-pool.idle-timeout-in-seconds = 300
            domain.resources.jdbc-connection-pool.oracle-pool.is-connection-validation-required = false
            domain.resources.jdbc-connection-pool.oracle-pool.is-isolation-level-guaranteed = true
            domain.resources.jdbc-connection-pool.oracle-pool.lazy-connection-association = false
            domain.resources.jdbc-connection-pool.oracle-pool.lazy-connection-enlistment = false
            domain.resources.jdbc-connection-pool.oracle-pool.match-connections = false
            domain.resources.jdbc-connection-pool.oracle-pool.max-connection-usage-count = 0
            domain.resources.jdbc-connection-pool.oracle-pool.max-pool-size = 32
            domain.resources.jdbc-connection-pool.oracle-pool.max-wait-time-in-millis = 60000
            domain.resources.jdbc-connection-pool.oracle-pool.name = oracle-pool
            domain.resources.jdbc-connection-pool.oracle-pool.non-transactional-connections = false
            domain.resources.jdbc-connection-pool.oracle-pool.pool-resize-quantity = 2
            domain.resources.jdbc-connection-pool.oracle-pool.property.password = dbpassword
            domain.resources.jdbc-connection-pool.oracle-pool.property.url = jdbc:oracle:thin:@localhost:1521:ORCL
            domain.resources.jdbc-connection-pool.oracle-pool.property.user = dbuser
            domain.resources.jdbc-connection-pool.oracle-pool.res-type = javax.sql.DataSource
            domain.resources.jdbc-connection-pool.oracle-pool.statement-timeout-in-seconds = -1
            domain.resources.jdbc-connection-pool.oracle-pool.steady-pool-size = 8
            domain.resources.jdbc-connection-pool.oracle-pool.transaction-isolation-level =
            domain.resources.jdbc-connection-pool.oracle-pool.validate-atmost-once-period-in-seconds = 0
            domain.resources.jdbc-connection-pool.oracle-pool.validation-table-name =
            domain.resources.jdbc-connection-pool.oracle-pool.wrap-jdbc-objects = false

Changing a property

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin set domain.resources.jdbc-connection-pool.oracle-pool.steady-pool-size=20
            domain.resources.jdbc-connection-pool.oracle-pool.steady-pool-size = 20

[複製到剪貼簿]

CODE:

AS_INSTALL_ROOT/bin/asadmin get domain.resources.jdbc-connection-pool.oracle-pool.steady-pool-size     
            domain.resources.jdbc-connection-pool.oracle-pool.steady-pool-size = 20

3) Using sun-resources.xml templatessun-resources.xml can be used to create jdbc-connection-pool & resources.
Templates for various databases will be available in GLASSFISH_INSTALL/lib/install/templates/resources/jdbc
Copy and modify the templates with appropriate values.
eg:
copy "oracle_type4_datasource.xml"  as oracle-resource.xml and modify the URL, user, password values.

[複製到剪貼簿]

CODE:

GLASSFISH_INSTALL/bin/asadmin add-resources GLASSFISH_INSTALL/lib/install/templates/resources/jdbc/oracle-resource.xml
            =========================
            Added Resource Type: jdbc-connection-pool
            =========================
            Added Resource Type: jdbc-resource
            Command add-resources executed successfully.

4)
Creating Connection Pool & Resource programmaticallyUsing AMX
(Application Server Management Extenstions) resource/pool can be
created. Following snippet
will create a connection pool, resource and resource-ref for the "server" instance.
[needs appserv-ext.jar in classpath]

[複製到剪貼簿]

CODE:

import java.util.Map;
            import java.util.HashMap;
            import com.sun.appserv.management.client.AppserverConnectionSource;
            import com.sun.appserv.management.DomainRoot;
            import com.sun.appserv.management.config.*;
            public class ResourceCreator {
                  public static final String hostName = "localhost";
                  public static final int JMX_PORT = 8686;
                  public static final String user = "admin";
                  public static final String password = "adminadmin";
                  public static void main(String[] args) throws Exception {
                      AppserverConnectionSource appserver =
new AppserverConnectionSource(AppserverConnectionSource.PROTOCOL_RMI,
hostName, JMX_PORT, user, password, null);
                      String connPoolName = "mysql-con-pool";
                      String resourceName = "jdbc/mysql-resource";
                      DomainRoot domainRoot = appserver.getDomainRoot();
                      //check whether the con.pool already exists, else, create one
Map<String, JDBCConnectionPoolConfig> conPoolMap =
domainRoot.getDomainConfig().getJDBCConnectionPoolConfigMap();
                      if (conPoolMap.containsKey(connPoolName)) {
                          System.out.println("JDBC Connection pool : " + connPoolName + " already exists");
                      } else {
                          Map<String, String> map = new HashMap<String, String>();
                          map.put("property.user", "jagadish");
                          map.put("ResType", "javax.sql.ConnectionPoolDataSource");
                          map.put("property.password", "jagadish");
                          map.put("property.databaseName", "jagadish");
                          map.put("property.url", "jdbc:mysql://localhost:3306/jagadish");
domainRoot.getDomainConfig().createJDBCConnectionPoolConfig(connPoolName,
"com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource", map);
                          System.out.println("JDBC Connection pool : " + connPoolName + " created ! ");
                      }
                      //check whether the jdbc resource already exists, else, create one
                      Map<String, JDBCResourceConfig> resMap = domainRoot.getDomainConfig().getJDBCResourceConfigMap();
                      if (resMap.containsKey(resourceName)) {
                          System.out.println("JDBC Resource : " + resourceName + " already exists");
                      } else {
                          domainRoot.getDomainConfig().createJDBCResourceConfig(resourceName, connPoolName, null);
                          System.out.println("JDBC Resource : " + resourceName + " created !");
                      }
                      //create a resource-ref for the instance "server"
 
                    Map<String, StandaloneServerConfig> map1 =
domainRoot.getDomainConfig().getStandaloneServerConfigMap();
                      {
                          StandaloneServerConfig sc = map1.get("server");
                          Map<String, ResourceRefConfig> rm = sc.getResourceRefConfigMap();
                          if (rm.containsKey(resourceName)) {
                              System.out.println(resourceName + " already referenced for the server instance");
                          } else {
                              sc.createResourceRefConfig(resourceName, true);
                              System.out.println(resourceName + " reference created for the server instance");
                          }
                      }
                  }
            }

References :GlassFish

Administration Guide > configuring connection pool

Administration Guide > configurations for specific drivers

AMX

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.