JDBC resource lookup from EJB
Friday Oct 26, 2007
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/derby.properties
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 SampleEjb.java : test() method
InitialContext ic = new InitialContext();
DataSource ds = (javax.sql.DataSource) ic.lookup("jdbc/cactus"
Connection con = ds.getConnection();
insert("something", ...., con);
con.close();
Create the appropriate Remote and Home interfaces.
4. Specifying the appropriate deployment descriptors:
Consider this is a web application.
application.xml
<application>
<display-name>Sample EJB Test</display-name>
<module>
<web>
<web-uri>sample-test.war</web-uri>
<context-root>/sample-test</context-root>
</web>
</module>
<module>
<ejb>sample-test.jar</ejb>
</module>
</application>
ejb-jar.xml
<ejb-jar>
<description>Sample test</description>
<display-name>Sample test </display-name>
<enterprise-beans>
<session>
<description> Sample bean </description>
<display-name> Sample bean </display-name>
<ejb-name>SampleRemote</ejb-name>
<home>examples.SampleRemoteHome</home>
<remote>examples.SampleRemote</remote>
<ejb-class>examples.SampleEjb</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
sun-ejb-jar.xml
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>SampleRemote</ejb-name>
<jndi-name>SampleRemote</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
web.xml
<web-app>
<distributable/>
<servlet>
<servlet-name>SampleTestServlet</servlet-name>
<display-name>Sample Test Servlet</display-name>
<servlet-class>servlets.SampleTestServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SampleTestServlet</servlet-name>
<url-pattern>/SampleTestServlet</url-pattern>
</servlet-mapping>
</web-app>
5. After all this coding, the structure would look like
/opt/sample
-src
-examples.SampleEjb
-examples.SampleRemoteHome
-examples.SampleRemote
-servlets.SampleTestServlet
-descriptor
-ear
-application.xml
-jar
-ejb-jar.xml
-sun-ejb-jar.xml
-web
-web.xml
-web
-index.jsp
-build.xml
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
Tags: connectors glassfishv2 jdbcresource










