Monday, February 6, 2012

Container Configuration - Glassfish

In this post, I will demonstrate configuring the various containers as we deploy our application to them.  For each container, we will need to configure the container managed connection pool and JNDI setup.  We will start with Glassfish 3.x.

Create the Data Source:
1. Start the glassfish admin (localhost:4848).
2. Click on Resources/JDBC/JDBC Connection Pools
3. Click New 









4. Fill in the Pool Name as : mysql_jsfdemodb_jsfdemouserPool
5. Fill in the Resource Type as: javax.sql.DataSource
6. Fill in the Database Vendor as: MySql
7. Click Next
8. For DataSource select the MySQLDataSource option (which is the default).
9. Fill in the additional properties as per the picture below.

10. Click Finish
You should now have the connection pool created. 

10. Click on Resources/JDBC/JDBC Resources
11. Click New

12. Fill in JNDI Name as: JSFDemoJNDI
13. Fill in Pool Name as: mysql_jsfdemodb_jsfdemouserPool
14. Click Finish
You should now have the JDBC datasource created.

OR: you could have used NetBeans to create a Glassfish JDBC resource.  The configured file creates a glassfish-resources.xml.  This file is used by NetBeans to configure Glassfish upon deployment, although, the file is not in your war file, nor is it directly copied to the server.  The glassfish-resources.xml 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">
<resources>
    <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="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" 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="mysql_jsfdemodb_jsfdemouserPool" 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="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="jsfdemodb"/>
        <property name="User" value="JSFDemoUser"/>
        <property name="Password" value="ItWorks!"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/jsfdemodb"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="JSFDemoJNDI" object-type="user" pool-name="mysql_jsfdemodb_jsfdemouserPool"/>
</resources>
If you use this method, it is advisable that after deployment, you look at the Glassfish admin and verify it properly created your resources.

This completes the Glassfish 3.x connection pool and JNDI configuration (twice actually).

2 comments:

  1. Hi, thanks the these posts. I am trying to build the project and I get the error:
    Could not find file C:\.....\JSFDemoApp\${libs.MyLibrary.classpath} to copy

    What does this library do? How can I solve the problem?

    Regards

    David

    ReplyDelete
    Replies
    1. Heh, I forgot about that little detail didn't I. This is simply the location I put all the third party libraries I used. The current contents of it for this project is:
      myLibraries/antlr-2.7.6.jar
      myLibraries/aopalliance.jar
      myLibraries/cglib-2.2.jar
      myLibraries/commons-collections-3.1.jar
      myLibraries/dom4j-1.6.1.jar
      myLibraries/hibernate-jpa-2.0-api-1.0.1.Final.jar
      myLibraries/hibernate3.jar
      myLibraries/javassist-3.12.0.GA.jar
      myLibraries/jta-1.1.jar
      myLibraries/mysql-connector-java-5.1.13-bin.jar
      myLibraries/overcast-1.0.1.jar
      myLibraries/slf4j-api-1.6.4.jar
      myLibraries/slf4j-nop-1.6.4.jar

      These are various libraries for jta, mysql, a primefaces theme called overcast, a logging library, hibernate, etc. I'll post a zip with them in it, although it will contain a few others simply because the other project expanded on these and I didn't separate the libraries. Note: if you download the jsfdemoapp_with_aop_compiled.zip, in JSFDemoApp_with_aop_compiled.zip\JSFDemoApp\build\web\WEB-INF\lib are all the libraries used by this project. But, it is good to note that I have some dependencies to my local setup I should remove. So thank you.

      --- Thomas

      Delete