DataBase Platform AutoDetection
By Mitesh Meswani on Jan 19, 2006
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 dialect.
Here is an example of a persistence.xml connecting to a Derby database:
<persistence ..... >
<persistence-unit .... >
<!-- JDBC connection properties -->
<property name="jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="jdbc.connection.string" value="jdbc:derby://localhost:1527/testdb;retrieveMessagesFromServerOnGetMessage=true;create=true;"/>
<property name="jdbc.user" value="yourusername"/>
<property name="jdbc.password" value="yourpassword"/>
<!-- SQL dialect to use -->
<property name ="toplink.platform.class.name" value = "oracle.toplink.essentials.platform.database.DerbyPlatform"/>
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 Detection" ?
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 detected platform>".
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,