Easy JBoss Connection Pooling with NetBeans IDE 4.1 and XDoclet

Create the application described in the "Use a Java Class to Access the Data Source" section in the Connecting to Databases chapter of "Using NetBeans IDE 4.1", which is currently a work in progress. It consists of a Java class that accesses one of the default PointBase databases and displays two fields ("name" and "city") on a JSP page.

Fast Track: Too lazy to create the application yourself? Or maybe you just want to see how JBoss connection pooling works with NetBeans IDE 4.1. Either way, you can download the application here. And then open it in the IDE and take all the steps below to quickly set up JBoss connection pooling.

Note that the org.me.customer.CustomerDAO class accesses the database via a data source called jdbc/poolDB. It uses this code to make the connection to the data source:

private javax.sql.DataSource getPoolDB() throws javax.naming.NamingException {
        javax.naming.Context c = new javax.naming.InitialContext();
        return (javax.sql.DataSource) c.lookup("java:comp/env/jdbc/poolDB");
}

(The code above was generated by the IDE, as described in this blog entry.)

The jdbc/poolDB data source referred to in the code above doesn't exist yet. In the Connecting to Databases chapter you can see how to set up a data source on the Sun Java System Application Server and on the Tomcat Web Server, since both servers are officially supported in NetBeans IDE 4.1.

In the following steps, you will create a data source on the JBoss server and access it from NetBeans IDE 4.1.

  1. Create the data source. In the JBoss installation directory, go to docs/examples/jca and copy pointbase-ds.xml to your JBoss deployment directory. (The pointbase-ds.xml file, along with many other database-server-specific templates, is kindly provided by the JBoss folks.) For this example, the JBoss deployment directory is its autodeploy directory: server/default/deploy. Now customize the pointbase-ds.xml template so that it looks as follows:

    <datasources>
       <local-tx-datasource>
          <jndi-name>jdbc/poolDB</jndi-name>
          <use-java-context>false</use-java-context>
          <connection-url>jdbc:pointbase://localhost:9092/sample</connection-url>
          <driver-class>com.pointbase.jdbc.jdbcUniversalDriver</driver-class>
          <user-name>pbpublic</user-name>
          <password>pbpublic</password>
          <metadata><type-mapping>PointBase</type-mapping></metadata>
      </local-tx-datasource>
    </datasources>

    That's it. You now have a JBoss data source. It is called jdbc/poolDB and provides access to the Sample database that is part of the default PointBase installation.

  2. Set the JBoss Port Number. Make sure that you like JBoss's port number. The default port number is 8080. This may conflict with other port numbers (especially if you have been playing with multiple servers). Therefore, go here in the JBoss installation directory and take a look:

    \\server\\default\\deploy\\jbossweb-tomcat50.sar\\server.xml

    Just search for "8080" in the file and change it to something else.

  3. Make the PointBase database driver available to JBoss. Do this by copying pbclient from the PointBase installation directory to the lib directory within the JBoss server's installation directory. For the JBoss autodeploy directory, the applicable lib directory is server/default/lib.
  4. Get Ant targets for starting and stopping JBoss from the IDE. Download servers-build.xml and servers-build.properties from Brian Leonard's article Integrating NetBeans with other J2EE Server Vendors. Put them in the same directory as where your application's build.xml file is found. Now open the build.xml file and add the following import statement after the existing import statement:

    <import file="servers-build.xml"/>
  5. Start JBoss from the IDE. Modify the jboss.home property in servers-build.properties and run the jboss-start target. Run it from inside the IDE (you can create a menu item, toolbar button, or shortcut key for it, as described in earlier blog entries). JBoss starts up and the Output window displays output received from JBoss. You'll see a lot of output and it might take a while. Somewhere near the end you should see something similar to the following (truncated here for easier reading):

    12:34:14,906 INFO  [WrapperDataSourceService] 
    Bound connection factory for resource adapter 
    for ConnectionManager 'jboss.jca:service=DataSourceBinding,name=jdbc/poolDB 
    to JNDI name 'jdbc/poolDB'

    This means that the jdbc/poolDB is available and that you can access it.

  6. Build the project to the JBoss autodeploy directory. Right-click the project in the Projects window to build it. (You can also build it in the Files window -- choose File > Set Main Project, set the current project as the main project, and click F11 whenever you want to build.) Modify the jboss-deploy target in servers-build.xml so that war.name is used instead of jar.name. Now run the jboss-deploy target. (If you haven't built the project, you'll get errors because the WAR file that the target tries to copy to the JBoss deployment directory hasn't been built yet.) This copies the application's WAR file to the JBoss autodeploy directory. In the Output window you should see something similar to the following:

    Copying 1 file to C:\\jboss\\jboss-4.0.1sp1\\server\\default\\deploy
    BUILD SUCCESSFUL (total time: 0 seconds)

    Now that the application is in the JBoss autodeploy directory, JBoss deploys it automatically...

  7. Start PointBase. In the IDE, choose Tools > Pointbase Database > Start Local Pointbase Database.
  8. Access the application's URL. Modify the jboss.client.url property in servers-build.properties so that it points to the JBoss port number and so that the name of the project replaces the name used by Brian's project. Now run the jboss-runapp target. This opens the IDE's default browser and displays the application as found in the JBoss autodeploy directory. You will get a 404 error message in your browser, with this helpful description: "The requested resource (/CustomerNameAndCity) is not available." Back in the IDE, run the jboss-stop target. Once JBoss has stopped, run the jboss-start target again. JBoss will produce this error in the Output window (truncated here for readability):

    javax.naming.NamingException: resource-ref: 
    jdbc/poolDB has no valid JNDI binding. 
    Check the jboss-web/resource-ref.

    This means... we need a jboss-web.xml file, containing a <resource-ref> section. We will use XDoclet to generate the file as well as its <resource-ref> section.

This is how to add XDoclet to the mix:

  1. Get XDoclet. Download and extract XDoclet 1.2.2.
  2. Get the Ant target for creating the jboss-web.xml file from the IDE. Download xdoclet-web-build and xdoclet-web-build.properties from Brian Leonard's article Integrating NetBeans with other J2EE Server Vendors. Put them in the same directory as where your application's build.xml file is found. Open the xdoclet-web-build.properties file and make sure the xdoclet.root.dir property is set appropriately. Now open build.xml and add the following import statement after the existing import statement:

    <import file="xdoclet-web-build.xml"/>
  3. Add @jboss comments to a Java class. In org.me.customer.CustomerDAO, add the following as comments right above the class declaration:

    \*  @jboss.resource-ref  res-ref-name="jdbc/poolDB"
    \*                       jndi-name="jdbc/poolDB"

    This must be in the right place -- i.e., not in the comments right at the top of the file, but in the comments that come in between the import statements and the class declaration.

  4. Customize and run the target from the IDE. Modify the webdoclet target in xdoclet-web-build.xml so that the fileset looks as follows:

    <fileset dir="${src.dir}">
        <include name="\*\*/\*\*.java"/>
    </fileset>

    Now run the webdoclet target. You'll see a nice big comment block in the Output window (thanks to Demi Moore) and when you go to your application's web/WEB-INF directory you will see your brand new jboss-web.xml file. This file contains the following important tags:

    <resource-ref>
      <res-ref-name>jdbc/poolDB</res-ref-name>
      <jndi-name>jdbc/poolDB</jndi-name>
    </resource-ref>
  5. Redeploy, including the jboss-web.xml file. Now build the project again, run the jboss-deploy target to deploy the WAR file to the JBoss deployment directory, and then run the jboss_runapp target again. You will now see the PointBase Sample database's "name" and "city" fields displayed in the IDE's default browser:

Comments:

i am using netbeans and tomcat server for jsp project the problem is that i am getting class not found error although i have added the oracle driver to netbeans driver as per the chapter using database of netbeans documentation.

Posted by piyush on March 26, 2006 at 06:29 PM PST #

Hi Piyush, maybe you also need to make the Oracle driver available to Tomcat, by putting it in its common/lib folder. If that doesn't work, you can write to met at geertjan.wielenga@sun.com, and then we'll try to work it out together.

Posted by Geertjan on March 26, 2006 at 06:31 PM PST #

in weblogic http://localhost:7001/console ok i want in jboss what " ". i want console deployement in jboss

Posted by rajj on April 26, 2006 at 06:05 PM PDT #

nalini

Posted by guest on September 13, 2006 at 09:46 PM PDT #

Hallo Geert-jan, Ik heb een probleem met een classNotFound exception van een oracle driver in Netbeans. Ik heb de laatste oracle driver (ojdbc14.zip) gedownload en in de volgende dir. geplaatst : C:\\Program Files\\netbeans-5.5\\enterprise3\\apache-tomcat-5.5.17\\common\\lib Zoals in de manual van netbeans beschreven heb ik mijn server.xml aangepast de datasource aan context.xml en web.xml toegevoegd. Toch krijg ik nog steeds een class not found exception... enig idee ? Als je tijd heb om deze vraag te beantwoorden heel graag.Al vast bedankt ! Groeten, Mark

Posted by Mark Bleichert on May 20, 2007 at 10:28 PM PDT #

Weet weinig over Oracle,maar als je mij een mailtje stuurt, naar geertjan DOT wielenga AT sun DOT com, stuur ik je door naar iemand die je misschien kan helpen.

Posted by Geertjan on May 20, 2007 at 10:30 PM PDT #

In web.xml making the following entry:
<web-app>
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

Next, add a file jboss-web.xml under WEB-INF/ folder of the web-application. This file should contain entry as:
<jboss-web>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:/jdbc/test</jndi-name>
<res-auth>Container</res-auth>
</resource-ref>
</jboss-web>

Next, in JBOSS_HOME/server/default/deploy folder make a xx-ds.xml file or else, a similiar file can be found in that same folder with the name hsqldb-ds.xml. Make following entries in that xml file:
<datasources>
<local-tx-datasource>
<jndi-name>/jdbc/test</jndi-name>
<type-mapping>MySQL</type-mapping>
<connection-url>jdbc:mysql://localhost:3306/shopping_cart</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root123</password>
<min-pool-size>1</min-pool-size>
<max-pool-size>5</max-pool-size>
</local-tx-datasource>

This should bind the jndi name.
In our client application, we need to look up as:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/jdbc/test");
Connection conn = ds.getConnection();

This works perfectly.

Posted by Rahul Nimbargi on April 06, 2008 at 08:59 PM PDT #

tryrty

Posted by guest on May 01, 2008 at 11:07 PM PDT #

Hi Regarding the post:

#############################################################################
In web.xml making the following entry:
<web-app>
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

Next, add a file jboss-web.xml under WEB-INF/ folder of the web-application. This file should contain entry as:
<jboss-web>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:/jdbc/test</jndi-name>
<res-auth>Container</res-auth>
</resource-ref>
</jboss-web>

Next, in JBOSS_HOME/server/default/deploy folder make a xx-ds.xml file or else, a similiar file can be found in that same folder with the name hsqldb-ds.xml. Make following entries in that xml file:
<datasources>
<local-tx-datasource>
<jndi-name>/jdbc/test</jndi-name>
<type-mapping>MySQL</type-mapping>
<connection-url>jdbc:mysql://localhost:3306/shopping_cart</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root123</password>
<min-pool-size>1</min-pool-size>
<max-pool-size>5</max-pool-size>
</local-tx-datasource>

This should bind the jndi name.
In our client application, we need to look up as:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/jdbc/test");
Connection conn = ds.getConnection();

This works perfectly.

#############################################################################

I am new to jboss deployments so which web.xml are you referring to?

In my application I have a web.xml file under the WEB-INF directory of the war file and
the tags there is web-app. I am a bit mixed up with the config files.
Can you please clarify more about what each file is responsible for and where is it supposed to be located?

Thank you

Posted by Meena on January 18, 2009 at 10:55 PM PST #

Hi,
You are correctly referring to the web.xml file. It is the same under WEB-INF directory of your war file.
As per your query, in <web-app> tag, please add the <resource-ref> tag & its contents as shown above in my previous post.
Also, add a new xml file named as 'jboss-web.xml', if it doesnot exist already under your WEB-INF folder of your war file.
The contents of the same are also given above. Please copy-paste it in your jboss-web.xml file.

web.xml provides configuration and deployment information for the Web components that comprise a Web application.

jboss.xml is the vendor-specific configuration file for JBOSS app server. It should be added to describe app server specific configurations relevant to your web application.

For using weblogic or websphere, there are similiar vendor specific xml files that need to be added. Please refer to their documentation.

An important point is that the specification does not allow you to add vendor-specific configuration in this file. While the web.xml is sufficient for most applications, there may be cases where you want to add JBoss-specific information in your deployment descriptor. This can be done by providing another file named jboss-web.xml in the WEB-INF directory.

Posted by Rahul Nimbargi on January 21, 2009 at 07:09 PM PST #

this is a very nice article

Posted by guest on February 02, 2009 at 04:25 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today