My previous post describes how to access a MySql datasource in JBoss from within a standalone java application. This is also supported by Glassfish and Sun Java System Application Server:
1. start MySQL server. If a
test
database is not present, create it (inside mysql console, run
create database test;
)
2. put MySQL Connector/J jar (mysql-connector-java-5.0.3-bin.jar) in
%SJSAS_HOME%\domains\domain1\lib\ext
, and restart Glassfish server.
3. create a jdbc-connection-pool and a jdbc-resource in admin web GUI, which is usually at http://localhost:4848. Setting
allow-non-component-callers
to true on the jdbc-connection-pool tells Glassfish to open this datasource for remote access, and its JNDI name will be used for remote lookup without
java:comp/env
prefix. Note that for GlassFish 3.x, allow-non-component-callers property is not needed for standalone client any more.
Alternatively, you can also do it in command line. Edit a file
\tmp\mysql-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems Inc.//DTD Application Server 9.0 Domain//EN"
"file:///C:/Sun/AppServer/lib/dtds/sun-resources_1_2.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="true"
name="mysql-pool"
datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"
non-transactional-connections="false"
res-type="javax.sql.DataSource" >
<property name="datasourceName" value="mysql"/>
<property name="user" value="root"/>
<property name="password" value="your-password"/>
<property name="port" value="3306"/>
<property name="databaseName" value="test"/>
<property name="serverName" value="localhost"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" pool-name="mysql-pool"
jndi-name="jdbc/mysql"/>
</resources>
Then run this command to create datasource:
%SJSAS_HOME%\bin\asadmin add-resources \tmp\mysql-ds.xml
4. java client is the same as
the one for JBoss, and compile it with the same command.
5. run it with JDK 6 so that I can use wildcard (*) in classpath and don't have to figure out which SJSAS jars to include (see my
previous post on this feature):
java -cp C:\ws\nb\scrap\build\classes;%SJSAS_HOME%\lib\*;%SJSAS_HOME%\imq\lib\*;%SJSAS_HOME%\domains\domain1\lib\mysql-java\mysql-connector-java-3.1.13-bin.jar
com.foo.DataSourceTest
Query 'select version()' returned 5.0.18
With JDK 1.5 or earlier, you need to explicitly list all jars in classpath. You need at least javaee.jar, appserv-rt.jar, and a jms-related jar in %SJSAS_HOME%\imq\lib. appserv-rt.jar contains a jndi.properties that configures the client-side naming service provider, and is equivalent to build\classes\jndi.properties in
JBoss example. I couldn't find out which jms jar right away, so I use the shortcut classpath format in JDK 6.
If the client and Glassfish server are not on the same host, or Glassfish naming service is not on the default port (3700), then add the two system properties to the above java command:
-Dorg.omg.CORBA.ORBInitialHost=remote-server-host-name
-Dorg.omg.CORBA.ORBInitialPort=server-naming-service-port
GlassFish 3.x provides a
gf-client.jar
, which is basically empty itself but references (via its
META-INF/MANIFEST.MF
) ALL GlassFish jar files. In GlassFish 3.1, it's in
$GLASSFISH_HOME/lib/gf-client.jar
; in GlassFish 3.0.1, it's
$GLASSFISH_HOME/modules/gf-client.jar
. One can use it in client classpath to include all GlassFish jars. The following is examples with GlassFish 3.1:
javac -cp $GLASSFISH_HOME/lib/gf-client.jar:$GLASSFISH_HOME/domains/domain1/lib/ext/mysql-connector-java-5.1.15-bin.jar:. test/DataSourceTest.java
java -cp $GLASSFISH_HOME/lib/gf-client.jar:$GLASSFISH_HOME/domains/domain1/lib/ext/mysql-connector-java-5.1.15-bin.jar:. test.DataSourceTest