Saturday, July 25, 2009

Creating Oracle Connection in Glassfish

1. Click on "JDBC" under "Resources" on "Common Tasks" on the left hand side in the admin console of GlassFish.
2. Click on "Connection Pools" on the right pane.
3. Click on the "New" button.
4. Enter a name for the connection pool, select Resource Type: javax.sql.DataSource, select Database Vendor: Oracle
5. Delete all properties, and add following properties:
  • user - set this to Oracle userid
  • password - set this to Oracle password
  • URL - set this to the URL, example jdbc:oracle:thin:@localhost:1521:DATABASENAME
6. Save the connection pool.
7. Now, create a new JDBC Resource, which will have a JNDI name to which your application will refer to.
8. Configuration settings required in the web.xml file and sun-web.xml:
  • unlike tomcat you wont need a \META-INF\context.xml file.
  • configure the JNDI for your application in web.xml, following is an example:
    • <resource-ref>
      <res-ref-name>jdbc/OracleTest</res-ref-name>
      <res-type&gt;javax.sql.DataSource</res-type>
      <res-auth&gt;Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>
  • configuration for sun-web.xml:
    • <resource-ref>
      <res-ref-name>jdbc/OracleTest</res-ref-name>
      <jndi-name&gt;jdbc/OracleTest</jndi-name>
      </resource-ref>
jdbc/OracleTest is the JNDI name configured for the Connection Pool that we have created above in the admin colsole of GlassFish.
You can now use java:comp/env/jdbc/OracleTest for look up and connect to the database.

2 comments:

Anonymous said...

EXCELENT THANKS

Alcides Benitez said...

Usefull information! blesses..

Post a Comment