I tried with a simple data source in Spring, but it isn't meant for production environments. So I wanted to move from:
<bean id="simpleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/database"/>
<property name="username" value="database"/>
<property name="password" value="s3cr3t"/>
</bean>
to:
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/databasePool" />
</bean>
where jdbc/databasePool is configured in Glassfish. The reason for this is that the connection handling is a lot better when using a proper connection pool.
First we need to install the mysql library in Glassfish. I use Ubuntu so:
sudo apt-get install libmysql-java
then I linked it into the Glassfish classpath:
sudo -u glassfish ln -s /usr/share/java/mysql.jar /opt/glassfishv3-prelude/glassfish/lib/mysql.jar
If you don't use the Ubuntu just add the mysql.jar to the /opt/glassfishv3-prelude/glassfish/lib folder.
Then restart the server to get it added to the classpath. (Look at my previous post for further instructions.)
Then install the connection-pool:
./asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlDataSource --allownoncomponentcallers=true --property user=database:password=s3cr3t:DatabaseName=database:ServerName=localhost:port=3306 database-pool
The --allownoncomponentcallers=true parameter is important to let Spring access the resource directly.
Test the connection:
./asadmin ping-connection-pool database-pool
To make it avaliable to the spring configuration:
./asadmin create-jdbc-resource --connectionpoolid database-pool jdbc/databasePool
That's it! Good luck!