Home > General > Application Scoped Resources : glassfish-resources.xml

Application Scoped Resources : glassfish-resources.xml

I recently created a Web project and wanted to deploy on Glassfish. For the database connection I created in Glassfish Admin Console JDBC Connection Pool and JDBC Resources. Everything works beautifully. But it is hard to go each time the Admin Console and make these settings.
It would be easier if we could manage database resources in the application.
Then I found the glassfish-resources.xml file where you can JDBC Connection Pool and JDBC Resources define exactly how the Admin Console.

The glassfish-resources.xml file looks like in my case;

<?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 name="java:app/myConnectionPool"
      res-type="javax.sql.ConnectionPoolDataSource"
      datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">

      <property name="User" value="root" />
      <property name="Port" value="3306" />
      <property name="DatabaseName" value="my_db" />
      <property name="ServerName" value="localhost" />
      <property name="Url" value="jdbc:mysql://localhost:3306/my_db"/>
      <property name="URL" value="jdbc:mysql://localhost:3306/my_db"/>
      <property name="Password" value="passwd" />
   </jdbc-connection-pool>

   <jdbc-resource enabled="true"
      jndi-name="java:app/jdbc/myDatasource"
      object-type="user"
      pool-name="java:app/myConnectionPool">
      <description />
   </jdbc-resource>
</resources>

Unlike Admin Console needs for “jdbc-connection-pool-name” and “jdbc-resource-jndi-name” the prefix “java:app/” given. Otherwise you get the following error message that I’ve been looking for.

Deployment Error for module: myWebApp : Error occurred during deployment: Exception while preparing the app : Invalid resource : { ResourceInfo : (jndiName=jdbc/myDatasource__pm), (applicationName=myWebApp) }. Please see server.log for more details.

The file can be copied in WEB-INF folder and the resource “java:app/jdbc/myDatasource” can be referred by the persistence.xml’s jndi-name attribute. A matching persistence.xml file for the  glassfish-resources.xml file looks like this;

<persistence-unit name="myWebPU" transaction-type="JTA">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <jta-data-source>java:app/jdbc/myDatasource</jta-data-source>

   <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
   </properties>
</persistence-unit>

For more information:

http://blogs.oracle.com/JagadishPrasath/entry/application_scoped_resources_in_glassfish

 

Advertisements
Categories: General Tags:
  1. Nancy
    5. October 2011 at 03:50

    Thanks for the share!
    Nancy.R

  2. Vuk
    29. August 2013 at 10:58

    Hey, where is glassfish-resources.xml located?

  3. Natarajan
    25. December 2013 at 12:27

    After enabling this xml in the WEB-INF we need to delete the jdbc resource and jdbc pooling in the Admin Console ?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: