DataBase Platform AutoDetection

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 dialect.

Here is an example of a persistence.xml connecting to a Derby database:
<persistence .....   >
    <persistence-unit .... >
        <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
         .
         .
        
        <properties>
            <!-- 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"/>
            .
            .
            .
        </properties>
    </persistence-unit>
     .
     .
     .
</persistence>


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").
e.g.
<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 :
  1.  java.sql.DatabaseMetaData#getDatabaseProductName() is called on the connection to get the database product name
  2. 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

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Mitesh Meswani

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks