persistence@glassfish.java.net

[Fwd: Proposed code changes to automatically detect database platform]

From: Pramod Gopinath <Pramod.Gopinath_at_Sun.COM>
Date: Wed, 14 Dec 2005 19:38:54 -0800

Resending message to include persistence_at_glassfish alias.

-------- Original Message --------
Subject: Proposed code changes to automatically detect database platform
Date: Wed, 14 Dec 2005 19:36:17 -0800
From: Pramod Gopinath <pramod.gopinath_at_sun.com>
To: Peter Krogh <peter.krogh_at_oracle.com>
CC: Marina Vatkina <Marina.Vatkina_at_Sun.COM>, "ejb3-toplink-ext_at_sun.com"
<ejb3-toplink-ext_at_Sun.COM>, Eliot Morrison <Eliot.Morrison_at_Sun.COM>,
Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
References: <20051206160300036.00000002188_at_pkrogh-lap>
<439F675F.30208_at_sun.com> <43A03AE6.7080300_at_oracle.com>
<43A078F5.7090207_at_sun.com> <43A08D56.3090003_at_sun.com>



Hi Peter
  Here are a set of proposed changes that would help us automatically
detect the database platform for both inside container as well as in the
javaSE case.

*Summary of changes :*
1. If the property "toplink.platform.class.name" is provided then that
would be the database platform (as is the current behavior).
2. If this property is not defined, map the vendor name returned by
DatabaseMetaData to the appropriate database platform.

Files Changed/Added:
1. oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl
      in updateServerSession(ServerSession ss, Map m)
          if the toplink.platform.class.name is not defined
              detect the platform name using a new method
getDBName(DatasourceLogin login)
                     which eventually delegates to a static method
                                   DBPlatformHelper.getDBPlatform(dbName)


2. Added a new class
oracle.toplink.essentials.platform.database.DBPlatformHelper
    This class maps vendor name to the appropriate db platform name
using a property file "VendorNameToPlatformMapping.properties".

3. A property file
oracle.toplink.essentials.platform.database.VendorNameToPlatformMapping.properties
used in 2. above

4. updated entity-persistence/build.xml to package the property file as
part of the "assemble" target.

*Questions on this proposed changes :
*1. To get a connection to the database we use
            java.sql.Connection conn = (java.sql.Connection)
login.connectToDatasource(null);
  Is this the correct approach ?
2. When running this code in the javaSE environment noticed that
     
oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl#updateServerSession()
    is being called twice.
   Is this correct ?


The idea behind trying to solve this issue was to do away with the need
for specifying the platform in the persistence.xml. Also this should not
cause any backward compatibility issue. If you could do the code review
we could try to get this in before the code freeze date (12/19).

In case you need any clarification we could have a con call.

Thanks
Pramod
 
 




? platform/database/DBPlatformHelper.java
? platform/database/VendorNameToPlatformMapping.properties
Index: internal/ejb/cmp3/EntityManagerSetupImpl.java
===================================================================
RCS file: /cvs/glassfish/entity-persistence/src/java/oracle/toplink/essentials/internal/ejb/cmp3/EntityManagerSetupImpl.java,v
retrieving revision 1.8
diff -r1.8 EntityManagerSetupImpl.java
52a53
> import oracle.toplink.essentials.platform.database.DBPlatformHelper;
92a94
>
434,435c436,437
< if (toplinkPlatform != null && !toplinkPlatform.equals("")) {
< login.setPlatformClassName(toplinkPlatform);
---
>         if (toplinkPlatform == null || toplinkPlatform.equals("")) {
>             toplinkPlatform = getDBPlatform(login);        
436a439,440
>         login.setPlatformClassName(toplinkPlatform);
>         
458a463,482
>     
>     private String getDBPlatform(DatasourceLogin login) {
>         java.sql.Connection conn = null;
>         try {
>             conn = (java.sql.Connection) login.connectToDatasource(null);
>             String dbName = conn.getMetaData().getDatabaseProductName();
>             String platformName = DBPlatformHelper.getDBPlatform(dbName);
>             return platformName;
> 
>         }  catch (java.sql.SQLException ex) {
>             //TODO: Need to i18n
>             throw new RuntimeException("Can not connect to database", ex);
>         }  finally {
>             try {
>                 if (null != conn)
>                 conn.close();
>             }  catch (java.sql.SQLException ex) {
>             }
>             
>         }
459a484
>     }





#
# The contents of this file are subject to the terms
# of the Common Development and Distribution License
# (the "License"). You may not use this file except
# in compliance with the License.
#
# You can obtain a copy of the license at
# glassfish/bootstrap/legal/CDDLv1.0.txt or
# https://glassfish.dev.java.net/public/CDDLv1.0.html.
# See the License for the specific language governing
# permissions and limitations under the License.
#
# When distributing Covered Code, include this CDDL
# HEADER in each file and include the License file at
# glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
# add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your
# own identifying information: Portions Copyright [yyyy]
# [name of copyright owner]
#

#Property file containing mapping between VendorName and VendorPlatform
(?i)oracle.*=oracle.toplink.essentials.platform.database.oracle.OraclePlatform
(?i)(sybase.*)|(adaptive\ server\ enterprise.*)|(SQL\ Server)=oracle.toplink.essentials.platform.database.SybasePlatform
(?i)microsoft.*=oracle.toplink.essentials.platform.database.SQLServerPlatform
(?i).*derby=oracle.toplink.essentials.platform.database.DerbyPlatform
(?i).*db2=oracle.toplink.essentials.platform.database.DB2Platform

(?i).*pointbase=oracle.toplink.essentials.platform.database.PointBasePlatform
(?i).*mysql=oracle.toplink.essentials.platform.database.MySQL4Platform

(?i)informix.*=oracle.toplink.essentials.platform.database.InformixPlatform