2/05/2011

A sample glassfish-resources.xml

A sample glassfish-resources.xml file that defines a global datasource and its underlying jdbc connection pool:

<?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="jdbc/test-pool" res-type="javax.sql.DataSource"
datasource-classname="org.apache.derby.jdbc.ClientDataSource"
pool-resize-quantity="1" max-pool-size="5" steady-pool-size="0"
statement-timeout-in-seconds="60" >
<property name="serverName" value="localhost" />
<property name="portNumber" value="1527" />
<property name="dataBaseName" value="sun-appserv-samples" />
<property name="User" value="APP" />
<property name="Password" value="APP" />
<property name="connectionAttributes" value=";create=true" />
<property name="driverType" value="4" />
</jdbc-connection-pool>

<jdbc-resource jndi-name="jdbc/test-ds" pool-name="jdbc/test-pool" />
</resources>
To create the declared jdbc resources with GlassFish asadmin command:
$ asadmin add-resources $HOME/glassfish-resources.xml
Command : JDBC connection pool jdbc/test-pool created successfully.
Command : JDBC resource jdbc/test-ds created successfully.
Command add-resources executed successfully.
To delete both resources:
$ asadmin delete-jdbc-connection-pool
--cascade true jdbc/test-pool
JDBC Connection pool jdbc/test-pool deleted successfully
Command delete-jdbc-connection-pool executed successfully.
To create, delete or list these jdbc resources in a cluster, simply add target option to the asadmin command:
$ asadmin add-resources --target cluster1 $HOME/glassfish-resources.xml
instance1:
Command : JDBC connection pool jdbc/test-pool created successfully.
Command : JDBC resource jdbc/test-ds created successfully.

instance2:
Command : JDBC connection pool jdbc/test-pool created successfully.
Command : JDBC resource jdbc/test-ds created successfully.

Command : JDBC connection pool jdbc/test-pool created successfully.
Command : JDBC resource jdbc/test-ds created successfully.
Command add-resources executed successfully.


$ asadmin list-jdbc-resources cluster1
jdbc/test-ds
Command list-jdbc-resources executed successfully.


$ asadmin list-jdbc-connection-pools cluster1
__TimerPool
DerbyPool
jdbc/test-pool
Command list-jdbc-connection-pools executed successfully.


$ asadmin delete-jdbc-connection-pool --cascade true --target cluster1 jdbc/test-pool
CLI031 Warning: Option "target" is obsolete and will be ignored.
JDBC Connection pool jdbc/test-pool deleted successfully
instance1:
JDBC Connection pool jdbc/test-pool deleted successfully

instance2:
JDBC Connection pool jdbc/test-pool deleted successfully

Command delete-jdbc-connection-pool executed successfully.
You can also create jdbc resources in GlassFish admin console, or via asadmin specifying command-line options. More details are in How to create jdbc connection pool and DataSource in GlassFish

Another usage of glassfish-resources.xml is to include it inside application, e.g., WEB-INF/glassfish-resources.xml for web app, META-INF/glassfish-resources.xml for ejb jar, etc. The declared resources are created upon deployment, and disposed of upon undeployment. More details in GlassFish embedded, JPA, EJB, DataSource and glassfish-resources.xml

5 comments:

Ecommerce Solutions said...

Hmmm, good to know about glassFish resources..thank you for the post..

Anonymous said...

Your blog is like the only place on the internet with examples for these files. You rock.

Anonymous said...

It should be noted that with the glassfish-resources.xml file containing the Pool Name that if Connection Pool is changed using the Glassfish Admin Console, when the project with the glassfish-resources.xml is deployed it will automatically CHANGE the Pool Name back to that which is in the project's glassfish-resources.xml file.

Kevin Rahe said...

The ability to create a connection pool and JDBC resource from a configuration file is great, but Glassfish's propensity to hang onto a reference to that file and update its internal definitions whenever the external file is changed is an anchor too big for the boat, which sinks it.

I need to specify a username and password with my JDBC connections, but it's bad enough having them appear in plain text in the Glassfish admin utility without needing to maintain them in an external plain text file as well.

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai