Declare an XWiki Data Source in GlassFish
Steps
Create the jdbc/xwiki data source that Use a Container-Managed Data Source asks for, so that GlassFish opens XWiki's database connections instead of XWiki opening them itself. The commands below use MySQL.
- Copy the MySQL JDBC driver into GLASSFISH_HOME/glassfish/domains/domain1/lib, and not into the web application's WEB-INF/lib, so that GlassFish itself can load it. GlassFish picks it up without a restart since version 7.0.18; on an earlier release, restart the domain with asadmin restart-domain.
- Create the connection pool. Quote the property list and escape every colon inside it with a backslash, since asadmin reads the colon as its property separator:
GLASSFISH_HOME/bin/asadmin create-jdbc-connection-pool \ --datasourceclassname com.mysql.cj.jdbc.MysqlConnectionPoolDataSource \ --restype javax.sql.ConnectionPoolDataSource \ --property 'User=xwiki:Password=xwiki:URL=jdbc\:mysql\://localhost\:3306/xwiki' \ jdbc/xwikipool - Check that the pool reaches the database:
GLASSFISH_HOME/bin/asadmin ping-connection-pool jdbc/xwikipoolIt answers Command ping-connection-pool executed successfully. If it fails instead, delete the pool with asadmin delete-jdbc-connection-pool jdbc/xwikipool and create it again with corrected values.
- Create the JDBC resource, naming it jdbc/xwiki:
GLASSFISH_HOME/bin/asadmin create-jdbc-resource --connectionpoolid jdbc/xwikipool jdbc/xwiki - List the resources:
GLASSFISH_HOME/bin/asadmin list-jdbc-resourcesjdbc/xwiki appears beside GlassFish's own jdbc/__TimerPool and jdbc/__default. Point XWiki at it by following Use a Container-Managed Data Source.
FAQ
Does XWiki need a container-managed data source?
No. XWiki opens its own connections from WEB-INF/hibernate.cfg.xml, and the resource-ref element a container data source needs is commented out in WEB-INF/web.xml.
XWiki does not find the data source. What is wrong?
A java:comp/env name resolves only for a resource declared as a resource-ref in WEB-INF/web.xml. Either uncomment the one XWiki ships and match its res-ref-name, or set hibernate.connection.datasource to the global name jdbc/xwiki.