Tuesday Dec 29, 2009

Applying Scope for JDBC Resources in GlassFish

JDBC Resources With Scope in GlassFish

Applying Scope for JDBC Resources in GlassFish

Java EE 6 introduces new set of application component environment namespace scopes.
Using the scope, an application can define (or export) a name such that it is visible for other components or modules within the application or even visible globally.

Scope:

  • java:comp : per component namespace
  • java:module : namespace shared by all components in a module (eg: ejb's in a ejb-jar.xml)
  • java:app : namespace shared by all components, modules in an application (eg: application-client, web component, ejb component in an application, .ear)
  • java:global : global namespace for all applications in the server

It is possible to define names with scope and thus make it available across the module, application or globally.

eg:
  • <res-ref-name> element's "name" attribute with prefixed value "java:comp/env/" will make the resource available only for the component in which it is defined (eg: Servlet or EJB or Application Client)
  • <res-ref-name> element's "name" attribute with prefixed value "java:module/env/" will make the resource available all the ejb components defined in its ejb-jar.xml (All EJBs in an ejb-jar.xml)
  • <res-ref-name> element's "name" attribute with prefixed value "java:app/env/" will make the resource available across all components/modules in the application (EJBs, Servlet, Application Client in the application)
  • <res-ref-name> element's "name" attribute with prefixed value "java:global" will  make the resource available across the server

By default, if no prefix is specified it will be equivalent to "java:comp"

Demo application :

The sample application provided demonstrates the usage of various scopes across the application,  by specifying resource-ref in deployment-descriptors. The sample has following components
  • web component [Servlet.java]
  • ejb component(s) [HelloStatefulEJB.java and HelloEJB.java]
  • application client [Client.java]
When executed for a particular resource name, the sample application will do lookup of the resource in Application Client, EJB(s), Servlet and based on the availability
of the resource in the component's scope it will either use the provided resource (datasource) or the default jdbc-resource (jdbc/__default).

Following are the resources defined (or exported) by the application.
  • java:global/env/Servlet_Resource
  • java:app/env/HelloStatefulEJB_Resource
  • java:module/env/HelloEJB_Resource
  • java:comp/env/Appclient_Resource

From the name, the scope of the resource, component in which the resource is defined can be identified.
In the following example :java:app/env/HelloStatefulEJB_Resource
Scope is "app"
Resource is defined for "HelloStatefulEJB"



Steps to execute:

  1. Ensure that JDK 1.6 is installed and the environment variable JAVA_HOME is set
    1. Ensure that the environment variable PATH has JAVA_HOME/bin added to it.
  2. Ensure that ANT is installed and the environment variable ANT_HOME is set
    1. Ensure that the environment variable PATH has ANT_HOME/bin added to it.
  3. Download GlassFish v3
  4. Install GlassFish v3
  5. Start GlassFish (asadmin start-domain domain1)
  6. Start Derby database (asadmin start-database)
  7. Download the sample
  8. Extracted location of sample is referred as "SAMPLE_HOME"
  9. goto SAMPLE_HOME
  10. set environment variable GF_HOME to GlassFish's installation location.
  11. "ant all" will compile, assemble, deploy, run the application and then undeploy the application.


Sample Output :

Existing build.xml is configured to demostrate the availability of the following resources across Application Client, Servlet, EJB(s)

  • java:global/env/Servlet_Resource
  • java:app/env/HelloStatefulEJB_Resource
  • java:module/env/HelloEJB_Resource
  • java:comp/env/Appclient_Resource


Sample output for
"java:module/env/HelloEJB_Resource" will look like :

     [exec] Mode : appclient
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : jdbc/__default
     [exec] RESULT                                        : RESOURCE NOT ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : servlet
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : jdbc/__default
     [exec] RESULT                                        : RESOURCE NOT ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : stateful_ejb
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : java:module/env/HelloEJB_Resource
     [exec] RESULT                                        : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec]
     [exec]
     [exec] Mode : stateless_ejb
     [exec] Resource name : java:module/env/HelloEJB_Resource
     [exec] ----------------------------------------------------------------------------------------------------------
     [exec] REQUESTED RESOURCE              : java:module/env/HelloEJB_Resource
     [exec] ACTUAL RESOURCE USED          : java:module/env/HelloEJB_Resource
     [exec] RESULT                                        : RESOURCE ACCESSIBLE
     [exec] ----------------------------------------------------------------------------------------------------------



In the above sample, "RESULT" indicate that the datasource "java:module/env/HelloEJB_Resource" is available only for the EJB components in the ejb-jar.xml ie., HelloEJB and HelloStatefulEJB.


References :



Tuesday Dec 15, 2009

DataSource Resource Definition in GlassFish v3

Java EE 6 introduces DataSource Resource Definition via Common Annotations Specification.
Refer the Enterprise tech tip on DataSource Definition published recently.

Wednesday Dec 02, 2009

Java EE Connector Architecture 1.6 Reference Implementation

GlassFish : Connector 1.6 Reference Implementation

Java EE Connector Architecture 1.6 Reference Implementation :


Connector 1.6 Specification is approved now ! Also refer the announcement by Siva. GlassFish v3 is the reference implementation of this version of the specification.
Developers can write resource-adapters leveraging the new features introduced in this specification and deploy the resource-adapters in
GlassFish v3. GlassFish v3 builds are available now !

Major features of Connector 1.6 specification available in GlassFish v3 :

  • Ease of Use : Annotation

    • It is now possible to develop a resource-adapter using various annotations defined in Connector 1.6, without deployement descriptor (ra.xml)
      • Refer the blog made available during GlassFish v3 Preview SDK release
  • Generic and Standard Work Context Mechanism

    • Generic work context contract enables a resource adapter to control the contexts in which the work instances submitted by it are executed by the application server's WorkManager.
      • Connector 1.6 mandates the following Work Context mechanisms to be supported by all application servers
        • Transaction Context (Similar to existing ExecutionContext)
        • Security Context 
        • Hints Context
  • Bean Validation support for resource-adapters

    • With Bean Validation mechanisms made available in Java EE 6 specification, Connector 1.6 allows resource-adapters to use Bean Validation mechanisms to validate various resource-adapter artifacts ie., ResourceAdapter, AdministeredObject, ManagedConnectionFactory, ActivationSpec JavaBeans.

Miscellaneous :

  • ClassLoading Requirements

    • Connector 1.6 specification recommends that the availability of resource-adapters to applications should be based on the reference of resource-adapter in the application's deployment-descriptor (or its equivalent annotations). These references can be via resource-ref (connector-resource), resource-env-ref (administered-object), resource-adapter-mid (for inbound). By default, GlassFish v3 supports the above recommendation.
  • Runtime Transaction Support

    • With the introduction of javax.resource.spi.TransactionSupport, a ManagedConnectionFactory artifact can dynamically request a transaction support level (XA/Local/NoTransaction) at runtime to override the value specified via deployment-descriptor (or its equivalent annotation)

In the coming weeks, you can look forward for more articles, tutorials, blogs on these features.

References :





About

jagadish

Search

Categories
Archives
« December 2009 »
SunMonTueWedThuFriSat
  
1
3
4
5
6
7
8
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
30
31
  
       
Today