TOTD #163: Application-scoped Resources in GlassFish using NetBeans

Guest Author

A globally defined resource such as JDBC connection pool, JDBC resource, JavaMail resource, Connector connection pools etc. in an application server is available globally to all the applications. This can cause resource starvation for a particular application due to usage by other applications. The application-scoped resources solves that problem by bundling the resource definitions along with the application. These resources are then created as part of the deployment and made available to the application, without the need for an explicit deployment script. These resources are automatically destroyed when the application is undeployed. These resources cannot be accessed from other applications and scoped to the deployed application only.

Jagadish blogged about app-scoped resources here. More details about this feature can be found in the Oracle GlassFish Server Online Docs.

This Tip Of The Day (TOTD) explains how to create application-scoped resources using NetBeans and deploy such an application in GlassFish.

  1. In NetBeans, create a Java EE 6 Web project by right-clicking in the “Projects” pane and selecting the category "Java Web", "Web Application". Name the project “AppScopedResources”. Click on “Next>” and take all the defaults.
  2. Right-click on the newly created project, select “New”, “Other...”, scroll to “GlassFish” and select “JDBC Resource”.

  3. Select “Create New JDBC Connection Pool” and take the default “JNDI Name” (or feel free to edit it) and click on “Next”.
  4. Click on “Next” as we don't need any additional properties.
  5. Change the “JDBC Connection Pool Name” to “myConnectionPool” and choose from an existing database connection as shown:

    The database connection in your IDE may be different. Click on “Next>”.

  6. Enter the username/password credentials and click on “Finish”.

    This generates “glassfish-resources.xml” under the newly created “Server Resources” tree in the “Projects” pane. The file looks like:
    <?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">
      <jdbc-resource enabled="true" jndi-name="jdbc/myDatasource" object-type="user" pool-name="myConnectionPool">
      <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="myConnectionPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="URL" value="jdbc:derby://localhost:1527/sun-appserv-samples"/>
        <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"/>

    If this application is deployed as is then the JDBC connection pool and resource are created in the global scope and are shared with other applications.
  7. Move “glassfish-resources.xml” to the “WEB-INF” directory. This ensures that the resources are created in the application-scope only.
  8. Right-click and select “Deploy”. In the “Services” pane, expand the GlassFish tree, “Resources”, “JDBC”, “JDBC Resource” and “Connection Pools”. This tree shows all the global JDBC connection pools and resources. The resources created as part of the application deployment are not shown here, as expected.

  9. In a shell, give the following commands:
    ~/tools/glassfish/3.1/glassfish3 >./bin/asadmin list-applications --resources
    AppScopedResources <web>
    java:app/jdbc/myDatasource <JdbcResource>
    java:app/myConnectionPool <JdbcConnectionPool>
    Command list-applications executed successfully.

    This shows that the resources created for us during the application deployment. The resources are deployed in “java:app” prefix which is defined by the Java EE 6 namespace. These commands can also be invoked for different sub-components of an application, such as a WAR file within an EAR, by giving the command:
    asadmin list-applications --subcomponents --resources

    and then the module-specific resources are shown accordingly.

  10. The database connection pool can be pinged using the following command:
    ~/tools/glassfish/3.1/glassfish3 >./bin/asadmin ping-connection-pool --appname AppScopedResources java:app/myConnectionPool
    Command ping-connection-pool executed successfully.

  11. The property values of the resources (such as connection pool size) can be changed using
    ~/tools/glassfish/3.1/glassfish3 >./bin/asadmin get applications.application.AppScopedResources.resources.jdbc-connection-pool.java:app/myConnectionPool.max-pool-size
    Command get executed successfully.

    And be changed using the “set” command. These properties can be preserved across multiple redeployments by specifying the following additional property:

    --properties preserveAppScopedResources=true

This is also explained in a screencast at GlassFishVideos.

You can try all these features in GlassFish 3.1. Download now!

Join the discussion

Comments ( 2 )
  • tim Tuesday, May 31, 2011
    Any chance for a EE6 / Glassfish demo in South Jersey / Philly?
  • guest Monday, June 20, 2011


    Is there a JUG in South Jersey or Philly ? I'll be happy to stop by there during my next visit to East Coast.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.