Labels

Wednesday, June 13, 2012

How to create DataSource and jdbc connection pool in GlassFish?

Every GlassFish DataSource depends on a connection pool that specifies how to connect to database. DataSource is just a thin layer on top of a connection pool. This is different from several other appservers.

Usually the easiest way is to do it in admin GUI at http://localhost:4848/ , choose Create New JDBC Connection Pools from the Common Tasks list on the front page right after login. You may need to scroll down a little bit to see it. On my screen, it is always hidden underneath the panel bottom. So for a while, I didn't notice its existence and always go to the left panel and choose Resources | JDBC | Connection Pools, which does the same with more clicks. The rest steps are self-explanatory.

For repeated configuration tasks, I prefer using GlassFish command line tool asadmin ($GLASSFISH_HOME/bin/asadmin). For example, this is script for creating mysql connection pool and datasource:

cp $HOME/mysql-connector-java-5.1.5-bin.jar $GLASSFISH_HOME/domains/domain1/lib/ext
 
$GLASSFISH_HOME/bin/asadmin stop-domain
$GLASSFISH_HOME/bin/asadmin start-domain
 
$GLASSFISH_HOME/bin/asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlDataSource --property user=root:password=test:DatabaseName=test:ServerName=localhost:port=3306 test-pool
 
$GLASSFISH_HOME/bin/asadmin create-jdbc-resource --connectionpoolid test-pool jdbc/test
 
 
To create a connection pool that supports distributed transaction, use com.mysql.jdbc.jdbc2.optional.MysqlXADataSource as datasourceclassname, and set --restype javax.sql.XADataSource option:

1
$GLASSFISH_HOME/bin/asadmin create-jdbc-connection-pool --restype javax.sql.XADataSource --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlXADataSource --property user=root:password=test:DatabaseName=test:ServerName=localhost:port=3306 test-pool
Run asadmin ping-connection-pool test-pool to verify whether the created connection pool can connect to the database. The database server needs to be running.

No comments:

Post a Comment