Using Parancoe with Glassfish

All you need for deploying an application developed with Parancoe is a servlet/JSP container, for example Tomcat.

Sometimes you may need a full J2EE application server:
  • because your application need some AS services
  • because you ISP provide only a full J2EE AS and not a standalone Tomcat
  • ...I don’t know…. (tell me)

The opensource Glassfish could be a common choice.

The only thing you have to change is the database configuration…and of course the particularities of your application.

For example, suppose you want to configure Glassfish and your application for using a Java DB (Apache Derby) database. As this 100% Java DBMS is included in JDK 6 and in Glassfish, it could be a common growing choice.

Create a connection pool to your DB

In the Glassfish Admin Console (http://localhost:4848/asadmin/) go to Resources/JDBC/Connection Pools, and add a new connection pool.

In the first step of the wizard:

glassfish_new_conn_pool_step1

In the next step, you have to validate the connection data from the first step.

Remember to check the “Allow Non Component Callers” flag:

glassfish_new_conn_pool_step1

and the properties of your database connection. In particular the database name, username and password. You can also set the “create” connection property to “true”, for auto-creating the database, if it doesn’t exist yet.

glassfish_new_conn_pool_step2-2

Then click the Finish button.

Add a JDBC Resource

In the Glassfish Admin Console (http://localhost:4848/asadmin/) go to Resources/JDBC/JDBC Resource, and add a new JDBC resource using the connection pool you just created:

glassfish_new_jdbc_resource

Now you can use this datasource in your Parancoe application.

Configure your Parancoe application

Usually all the database configuration is in the database.xml configuration file in the WEB-INF directory:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:parancoe="http://parancoe.dev.java.net/schema/parancoe" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://parancoe.dev.java.net/schema/parancoe https://jugpadova.dev.java.net/schema/parancoe.xsd">

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/myappDS"/>
    </bean>

    <!-- Derby session factory -->
    <bean id="sessionFactory" parent="abstractSessionFactory">
        <property name="hibernateProperties">
            <props merge="true">
                <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <!-- Authomatic discovering of persistent classes -->
    <parancoe:discover-persistent-classes basePackage="com.myapp"/>

</beans>

Note the JNDI name you use for your datasource it’s the same of the JDBC resource you just created.

Now You can deploy your application to glassfish and it will use that connection to your database. Of course you have also to startup your Java DB server.

Posted by Lucio Benfante on Wednesday, July 25, 2007