Thursday Nov 06, 2008

JDBC support in V3 Prelude

The latest application server of Sun – GlassFish V3 Prelude includes JDBC support. The JDBC Connection pooling infrastructure in V3 prelude is same as that of GlassFish 9.1 application server.

Some of the cool features that could be useful to you are :

  • Connection Leak Tracing

    •  enable the connection-leak-timeout to a certain value along with connection-leak-reclaim to true to avoid the potential connection leaks in your application

  • Validation calls optimization

    • Set the validate atmost once period to a value within which the validation will be carried out atmost once.

  • Associate with thread

    • To reuse connections in a multi threaded application to avoid the overhead of getting a connection from the pool.

and many more...

Things that have changed:

Setting the pool properties/attributes using asadmin set command would be like:

asadmin set --value=10 server.resources.jdbc-connection-pool.DerbyPool.steady-pool-size

asadmin set --value=newUser

Note that the first asadmin set does not need a server restart whereas the second one mandates restart.

There are many more attractive things coming up in the next release of V3 for JDBC. One of them is advanced pool monitoring. The monitoring framework has undergone a major change in v3 prelude.

Try the latest V3 prelude and its connection pooling features. Any issues? Mail .

Monday Oct 13, 2008

JDBC Pool Monitoring using Management Rules

After my basic blog blog about creating and writing mbeans and self management rules, here is a post on how to monitor a certain attribute of a jdbc connection pool using Self Management Rule.
I would take for example, the connection validation failures. The Monitoring framework of glassfish provides statistics on various attributes including an attribute specially for the number of connection validation failures. Here are the steps :

1. First of all, enable the monitoring for JDBC Connection Pool to HIGH from the admin console or from CLI.

2. Create JDBC Connection Pool/JDBC Resource for this exercise : conValPool and jdbc/conval respectively.

3. We would be monitoring the attribute for connection validation failure on this pool conValPool.

4. The objectName for this conValPool for monitoring would be "com.sun.appserv:name=conValPool,type=jdbc–connection–pool,
I got this objectName from listing all mbeans and querying the names for conValPool. Note that this mbean is specifically for monitoring this pool since the category is monitor.

5. When i open jconsole and login to it, i see that com.sun.appserv  section, there is a head called conValPool. Under this, we see a server element if we browse down the tree. On the right pane, we can see all the attributes and i would take a note of the attribute with name numconnfailedvalidation–count.
I would use this attribute in my custom mbean event.

6. Create an mbean called ValidationMonitor for getting the value of numconnfailedvalidation–count at any point of time. It would have code that gets the value as follows

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
numConnFailVal = (Long) server.getAttribute(new
    category=monitor,server=server”) , "numconnfailedvalidation–count";)

I use the getAttribute method on the MBeanServer with the above mentioned ObjectName and the attribute name. The numConnFailVal is the attribute that will be returned whenever this mbean is invoked.

7. Create an mbean called ValidationAction for the action part of the management rule. This action would be triggered every time some threshold is crossed as part of the rule that we are going to create shortly. The action as of now is going to be a simple System.out.println  to the server.log as follows

public class ValidationAction implements NotificationListener,
    com.sun.example.action.ValidationActionMBean {
    public synchronized void handleNotification(Notification notification, Object handback) {
         try {
System.out.println("Validation  Failure Threshold Notification: " +
          } catch(Exception ex) {

Note that the action mbean implements the NotificationListener interface.

8. Coming to the part of creating a management rule to hook up this action and event, we log into the admin console and deploy the custom mbeans created above.
Then under the Management Rules link under Configuration, create a new management rule called ValFailRule. Specify the event type as monitor and other values as follows

observed MBean value : user:impl–class–name=com.sun.example.mbeans.ValidationMonitor,

observedAttribute value : numConnFailVal
Monitor type to Counter
Granularity period to 1000 milliseconds so that every second the rule would check for the numConnFailVal value.
Number type to long since the return value from our mbean is of type long.
Init Threshold value to 2 so after 2 failed validations, the rule is going to write something to the
server log.
Offset to 1
Select the validationaction from the list under Action section for the action mbean name.

9. The management rule is successfully setup and once you have a test running that would change the validation failure values, the rule would play its role. Whenever the numConnFailVal value goes above 2, the following can be seen in the server.log

Validation  Failure Threshold[

43f6a09f5ae1be9f0e51;|Logging selfmanagement event :

SEVERE messages are because the log level was set to SEVERE while the rule was created.

This is just an example of how a JDBC pool monitoring attribute can be observed via mbeans and action taken above a threshold using a management rule. Any other meaningful action could be taken instead of the above.

Hope this is useful. :)

Friday May 16, 2008

GlassFish V3 - Using JDBC Connection Pool / JDBC Resource / MySQL

This blog talks about creating a JDBC Connection Pool and an associated JDBC resource with MySQL as the Database vendor.

default database "sakila" of MySQL is used in this example. All these
could be done so easily with Netbeans 6.1 after installing the
GlassFish V3 plugin. 

This screencast explains how to create a JDBC Connection pool in the
administration console and its associated JDBC resource. It also shows
a HelloWorld web application that used this resource to get some values
from a table in sakila database.

Watch the screencast :

GlassFish V3 TP2 JDBC

Download GlassFish V3 TP2 from

Friday Oct 26, 2007

Lookup from Standalone Client

My previous blog was about accessing a JDBC resource from an ejb. 

This blog talks about accessing the JDBC resource i created in the last blog, from a standalone client.

I use the same configuration from my previous blog : jdbc/cactus for my resource that needs to be accessed.

My sample will have the following code:

InitialContext ctx = new InitialContext();

javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/cactus";)

Connection c = ds.getConnection();



Compile the sample

 Run the sample

 Note the included jars in the classpath

  • appserv-rt.jar
  • javaee.jar
  • appserv-admin.jar
  • imqjmsra.jar
  • derbyclient.jar

Also, observe the system properties for the server host and naming service port.

JDBC resource lookup from EJB

Accessing a JDBC resource from an EJB is a fairly simple thing to do. As simple as the following:

1. Start domain and database

After installing GlassFishV2, start the domain domain1 and start the database. I would be using Derby in this example. Derby can be started in 2 ways    

  • <INSTALL_ROOT>/bin/asadmin start-database
  • From netbeans : Tools -> Java DB Database -> Start JavaDB Server. The port settings for the server started up can be modified in ~/.netbeans-derby/

 Now, the database is started at port :1527 and domain is running at 4848.

2. Create a JDBC Connection pool and JDBC Resource

 Login to the Admin Console (http://localhost:4848) and navigate to Resources->JDBC->ConnectionPools. Click on New and enter the details like Name:Cacti  ResourceType:javax.sql.DataSource DatabaseVendor:Derby. In the next screen, accept the default values but don't forget to enter the following additional properties

  • DatabaseName : sampleDB
  • ConnectionAttributes : create=true (if the sampleDB does not exist)
  • Password : dbpassword
  • PortNumber : 1527 (or the customized DB port)
  • ServerName : localhost
  • User : dbuser

Click on Finish. To verify, click on the 'Cacti' connection pool you just created and do a 'Ping connection Pool".

 Navigate to Resources->JDBC->JDBCResource on the left panel of the admin console and click on New. Specify a JNDI name as "jdbc/cactus". I chose to append the name cactus with jdbc since i am accessing a database. Choose the pool name you just created "Cacti" and click on 'OK'.

3. Access the resource in your app

Suppose i have an EJB that has to use this JDBC resource. This can be done as follows in the : test() method

InitialContext ic = new InitialContext();
DataSource ds = (javax.sql.DataSource) ic.lookup("jdbc/cactus";)

Connection con = ds.getConnection();

insert("something", ...., con);


Create the appropriate Remote and Home interfaces.  

4. Specifying the appropriate deployment descriptors:

Consider this is a web application.


    <display-name>Sample EJB Test</display-name>


    <description>Sample test</description>
    <display-name>Sample test </display-name>
          <description> Sample bean </description>
          <display-name> Sample bean </display-name>




        <display-name>Sample Test Servlet</display-name>

5. After all this coding, the structure would look like


Build this application, create an ear and deploy this application on the application server via the admin console.

6. The Application can be accessed via http://localhost:8080/sample-test




« September 2016