Method 1: create a connection pool mypool in the management console. Enter the Management Console address: localhost: 4848 in the browser. Use the default username & ldquo; admin & rdquo; and its password & ldquo; adminadmin & rdquo; to log on. After successful logon, expand & ldquo; Resource & rdquo;-& ldquo; JDBC & rdquo;-in the tree menu on the left ;-
Method 1: Use the Console
First, create a connection pool mypool on the console. Enter the Management Console address: localhost: 4848 in the browser. Use the default username "admin" and its password "adminadmin" to log on. After successfully logging on, expand "resource"-"JDBC"-"connection pool" in the tree menu on the left ".
On the main panel, click "new ". In the "Create JDBC connection pool (step 1, 2 in total)" on the panel, enter "name" as mypool, and "Resource Type" as "javax. SQL. select "DataSource" and "database supplier" as "JavaDB ". In the next "Create a JDBC connection pool (step 2 in total)", you can see the default settings of the database connection pool. The "Idle timeout" value in the "pool Settings" column is changed from the default 300 to 777. Click "finish ". So far, we have created the database connection pool mypook through the console and modified its idle timeout value.
Method 2: Use the command line tool asadmin
Next, we can view this resource through asadmin of the command line.
Asadmin list server. resource *
The running result is as follows:
Undefined
Undefined
Server. resource
-
Ref
. Jdbc
/
_ CallFlowPool server. resource
-
Ref
. Jdbc
/
_ TimerPool server. resource
-
Ref
. Jdbc
/
_ Default server. resources server. resources. jdbc
-
Connection
-
Pool. DerbyPool server. resources. jdbc
-
Connection
-
Pool. _ CallFlowPool server. resources. jdbc
-
Connection
-
Pool. _ TimerPool server. resources. jdbc
-
Connection
-
Pool. mypool server. resources. jdbc
-
Resource. jdbc
/
_ CallFlowPool server. resources. jdbc
-
Resource. jdbc
/
_ TimerPool server. resources. jdbc
-
Resource. jdbc
/
_ Default
Mbeans listed here are identified by GlassFish's DottedName. Then, use the asadmin sub-Command get to view the properties of the object mypool:
Asadmin get server. resources. jdbc-connection-pool.mypool .*
Alternatively, you can view the attribute values of idle-timeout-in-seconds.
Asadmin get server. resources. jdbc-connection-pool.mypool.idle-timeout-in-seconds
The result is as follows:
Server. resources. jdbc-connection-pool.mypool.idle-timeout-in-seconds = 777
So far, we have completed the access to mypool using the command line management tool asadmin. Here, asadmin accesses MBean through the Dotted Name extension of GlassFish. Dotted Name is a set of conventions defined by the GlassFish command line tool asadmin. With the support of this set of conventions, the three sub-commands (list, set, and get) of asadmin can be addressed to mbeans in GlassFish through a string separated.
Method 3: Use the third-party tool JConsole
Next, we will access the object mypool through JConsole.
On the JConsole logon panel, select the remote process: localhost: 8686 (8686 is the default Management port of GlassFish). The username is admin and the password is adminadmin. After logging in, you will see information about the running time of the GlassFish Application Server. Click "MBean ". Expand the tree structure "com. sun. aperv"-"jdbc-connection-pool"-"my pool"-"config"-"attribute ".
Yes
The information of the Connection Pool mypool that we care about is displayed. The value of idle-timeout-in-seconds is 777. Change 777 to 888.
Return to the management console or the command line tool asadmin and you can see that the modification just made on JConsole has taken effect.
The above three tools are equivalent to the change to GlassFish resources.
Next, access the mypool of the database connection pool through programming.
Method 4: Standard JMX Programming
The standard JMX code is as follows: (The following is the Demo code. In order to highlight the key points, no exception handling is performed .)
Undefined
Undefined
Import javax. management.
*
; Import javax. management. remote.
*
;
Public
Class
JMX_demo {
Public
JMX_demo () throws Exception {
//
Create a JMX URL
JMXServiceURL
=
New
JMXServiceURL (
"
Service: jmx: rmi: // jndi/rmi: // localhost: 8686/jmxrmi
"
); Java. util. Map env
=
New
Java. util. Hashtable ();
//
Default username and password
String [] creds
=
{
"
Admin
"
,
"
Adminadmin
"
}; Env. put (JMXConnector. CREDENTIALS, creds );
//
Establish a connection
JMXConnector Conne
=
JMXConnectorFactory. connect (url, env); MBeanServerConnection mbsc
=
Connector. getMBeanServerConnection ();
//
Object Name of the MBean to be accessed
ObjectName mbeanName
=
New
ObjectName (
"
Com. sun. appserv: type = jdbc-connection-pool, name = mypool, category = config
"
);
//
The attribute idle-timeout-in-seconds to be accessed
System.
Out
. Println (
"
Using JMX, jdbc pool idle timeout:
"
+
Mbsc. getAttribute (mbeanName,
"
Idle-timeout-in-seconds
"
));}
Public
Static
Void
Main (final String [] args) throws Exception {
New
JMX_demo ();}}
The running result is as follows: Using JMX, jdbc pool idle timeout: 888
Undefined
Undefined
Public
Class
AMX_demo {
Public
AMX_demo () throws Exception {
//
Domain Admin Server machine name or IP address
Final String host
=
"
Localhost
"
;
//
JMX Management port. The default value is 8686.
Final
Int
Port
=
8686
;
//
Administrator name
Final String user
=
"
Admin
"
;
//
Administrator Password
Final String password
=
"
Adminadmin
"
; TLSParams tlsParams
=
Null
;
//
Connect to JMX server
AppserverConnectionSource conn
=
New
AppserverConnectionSource (AppserverConnectionSource. PROTOCOL_RMI, host, port, user, password, tlsParams,
Null
); Conn. getJMXConnector (
True
);
//
DomainRoot and JDBCConnectionPoolConfig are the DCP component DomainRoot mDomainRoot = conn. getDomainRoot ();
//
Get the JDBCConnectionPool list
Map pools
=
MDomainRoot. getDomainConfig (). getJDBCConnectionPoolConfigMap (); JDBCConnectionPoolConfig mypool
=
(JDBCConnectionPoolConfig) pools.
Get
(
"
Mypool
"
); System.
Out
. Println (
"
Using DCP, jdbc pool idle timeout:
"
+
Mypool. getIdleTimeoutInSeconds ());}
Public
Static
Void
Main (final String [] args) throws Exception {
New
AMX_demo () ;}} Method 5 through AMX Programming
The AMX code is as follows:
Undefined
Undefined
Import com. sun. appserv. management. domainRoot; import com. sun. appserv. management. client. appserverConnectionSource; import com. sun. appserv. management. client. TLSParams; import com. sun. appserv. management. util. misc. exceptionUtil; import com. sun. appserv. management. config.
*
; Import java. ConnectException; import java. util. Map;