12/31/2008

GlassFish MySql connection pool with empty password

The default mysql database server password is empty, which is convenient in development mode, but turns out to be trouble when creating a jdbc connection pool in GlassFish. There seems to be no way to specify an empty password either in admingui or asadmin.

One workaround is to use a temporary password (say, "xxx") when creating the connection pool, stop the domain, open $GLASSFISH_HOME/domains/domain1/config/domain.xml, and replace "xxx" with ""

This should work with all versions of GlassFish (v1, v2, and v3). To do it with script:

$GLASSFISH_HOME/bin/asadmin start-domain

$GLASSFISH_HOME/bin/asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlXADataSource --property user=root:password=xxx:DatabaseName=test:ServerName=localhost:port=3306 mysql-pool
$GLASSFISH_HOME/bin/asadmin create-jdbc-resource --connectionpoolid mysql-pool jdbc/test

$GLASSFISH_HOME/bin/asadmin stop-domain

cd $GLASSFISH_HOME/domains/domain1/config
cat domain.xml | sed "s/xxx//" > domain.xml.replaced
mv domain.xml domain.xml.original
mv domain.xml.replaced domain.xml

$GLASSFISH_HOME/bin/asadmin start-domain
vi/vim is also well suited for this type of search-replace:
vim domain.xml
/xxx
dw
:wq

12/17/2008

Pass optional params with varargs

Varargs (introduced in Java SE 5) allows you to pass 0, 1, or more params to a method's vararg param. This reduces the need for overloading methods that do the similar things. For example,

package javahowto;

import org.apache.commons.lang.StringUtils;

public class HelloWorld {
public static void main(String[] args) {
hello();
hello("World");
hello("Santa", "Bye");

final String[] names = {"John", "Joe"};
hello(names);
}

public static void hello(String... names) {
final String defaultName = "Duke";
String msg = "Hello, ";
msg += ((names.length == 0) ? defaultName : StringUtils.join(names, ", "));
System.out.println(msg);
}
}
org.apache.commons.lang.StringUtils is in commons-lang-2.4.jar. Running this example gives the following output:
Hello, Duke
Hello, World
Hello, Santa, Bye
Hello, John, Joe
BUILD SUCCESSFUL (total time: 0 seconds)

12/07/2008

How to create jdbc connection pool and DataSource 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:
$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.

12/02/2008

Disable NetBeans httpmonitor in GlassFish

I found my GlassFish v2 server.log file is littered with these warning messages:

WARNING *********** NetBeans HTTP Monitor ************
The request cannot be recorded most likely because the
NetBeans HTTP Server is not running....
Here is what I just did to disable and completely remove NetBeans httpmonitor from my GlassFish v2:

1. Open $GLASSFISH_HOME/domains/domain1/config/default-web.xml, delete or comment out the declaration and mapping for servlet filter HTTPMonitorFilter. That is, the following lines:
<filter>
<filter-name>HTTPMonitorFilter</filter-name>
<filter-class>org.netbeans.modules.web.monitor.server.MonitorFilter</filter-class>
<init-param>
<param-name>netbeans.monitor.ide</param-name>
<param-value>127.0.0.1:8082</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>HTTPMonitorFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
2. Optionally, delete $GLASSFISH_HOME/domains/domain1/lib/org-netbeans-modules-web-httpmonitor.jar.
Since I don't need this httpmonitor, there is no reason to keep this jar.

3. Redeploy any web applications that was previously deployed when NetBeans HTTPMonitorFilter was in default-web.xml. This step is needed for apps already deployed, because the generated web.xml already contains HTTPMonitorFilter (inherited from the old default-web.xml), and it will not be regenerated even at server restart.

That should get rid of this warning messages. For more technical details, see NetBeans issue 139653 and GlassFish issue 3844.