Platform Auto Detection In Glassfish
Any persistence provider communicating with relational database needs
to generate SQL. Unfortunately, the SQL dialect to use varies slightly
from one database vendor to another. Typically the user would need to
provide a hint about the database vendor whose SQL dialect
is to be used. For example, Glassfish needs the property
“toplink.platform.class.name” to point to the class name that
corresponds to the database vendor being used.
If you look at a typical persistence.xml, you will notice that the
information about database vendor is specified twice:
- Connection properties either as data source or as jdbc properties
- Property "toplink.platform.class.name" to specify the SQL
Here is an example of a persistence.xml connecting to a Derby database:
<persistence-unit .... >
<!-- JDBC connection properties -->
<property name="jdbc.user" value="yourusername"/>
<property name="jdbc.password" value="yourpassword"/>
<!-- SQL dialect to use -->
<property name ="toplink.platform.class.name"
We have implemented a feature in Glassfish to automatically detect the
database vendor based on the connection properties (datasource or
jdbc). Internally this feature is called "Platform Auto Detection". By
introducing this feature, we have removed the above mentioned
duplication and consequently improved the end user experience. Below is
a FAQ on this feature.
How does one enable "Platform Auto
Omit the property "toplink.platform.class.name" from your
persistence.xml. If this property is not provided, it is a hint to
Glassfish to automagically detect the database vendor.
How do I know the platform that was
automatically detected by Glassfish Persistence runtime?
This can be achieved by enabling the logging level for Glassfish
Persistence runtime. When this logging level is set to "FINE" check the
log messages for a string that reads "Detected Vendor platform: <the
How does one enable the log level for
Glassfish Persistence runtime?
Define a property "toplink.logging.level" in your persistence.xml with
the desired log level ("INFO/FINE/FINEST").
<property name="toplink.logging.level" value="FINE"/>
Where can I find more information on
how this is implemented in Glassfish ?
Glassfish adopts a 2 step approach to achieve this :
- java.sql.DatabaseMetaData#getDatabaseProductName() is
called on the connection to get the database product name
- The product name is then matched against a set of regular
expressions to compensate for variations in the product name that are
returned by different jdbc drivers. This matched regular expression
maps to the appropriate database platform class name,
Further information can be be found in DBPlatformHelper.java