Skip to content

Commit

Permalink
Bug 518774: HANAPlatform is not detected; improving platform detection (
Browse files Browse the repository at this point in the history
#520)

Signed-off-by: Will Dazey <dazeydev.3@gmail.com>
Reviewed-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
dazey3 committed Sep 11, 2019
1 parent be65755 commit 2637c10
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2019 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -56,20 +57,23 @@ public class DBPlatformHelper {
* If vendorName does not match any of predefined vendor names, <code>
* DEFAULTPLATFORM </code> is returned.
*/
public static String getDBPlatform(String vendorName, SessionLog logger) {
public static String getDBPlatform(String vendorName, String minorVersion, String majorVersion, SessionLog logger) {

initializeNameToVendorPlatform(logger);

String detectedDbPlatform = null;
if(vendorName != null) {
detectedDbPlatform = matchVendorNameInProperties(vendorName, _nameToVendorPlatform, logger);
}
vendorName = (vendorName == null) ? "Vendor Not Found" : vendorName;
minorVersion = (minorVersion == null) ? "0" : minorVersion;
majorVersion = (majorVersion == null) ? "0" : majorVersion;

String vendor = vendorName + "[" + minorVersion + ", " + majorVersion + "]";

String detectedDbPlatform = matchVendorNameInProperties(vendor, _nameToVendorPlatform, logger);
if (logger.shouldLog(SessionLog.FINE) ) {
logger.log(SessionLog.FINE, SessionLog.CONNECTION, "dbPlatformHelper_detectedVendorPlatform", detectedDbPlatform ); // NOI18N
}
if (detectedDbPlatform == null) {
if(logger.shouldLog(SessionLog.INFO)) {
logger.log(SessionLog.INFO, SessionLog.CONNECTION, "dbPlatformHelper_defaultingPlatform", vendorName, DEFAULTPLATFORM); // NOI18N
logger.log(SessionLog.INFO, SessionLog.CONNECTION, "dbPlatformHelper_defaultingPlatform", vendor, DEFAULTPLATFORM); // NOI18N
}
detectedDbPlatform = DEFAULTPLATFORM;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2018 IBM Corporation. All rights reserved.
* Copyright (c) 1998, 2019 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -208,31 +208,25 @@ public void setDatasourceAndInitialize() throws DatabaseException {
* @param throwException - set to true if the caller cares to throw exceptions, false to swallow them.
*/
protected void setOrDetectDatasource(boolean throwException) {
String vendorNameAndVersion = null;
String vendorName = null;
String minorVersion = null;
String majorVersion = null;
String driverName = null;

// Try to set the platform from JPA 2.1 schema properties first before
// attempting a detection.
if (getProperties().containsKey(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME)) {
vendorNameAndVersion = (String) getProperties().get(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME);

String majorVersion = (String) getProperties().get(PersistenceUnitProperties.SCHEMA_DATABASE_MAJOR_VERSION);
if (majorVersion != null) {
vendorNameAndVersion += majorVersion;
}

// The minorVersion is not currently used in platform matching, but
// shouldn't change matching when added
String minorVersion = (String) getProperties().get(PersistenceUnitProperties.SCHEMA_DATABASE_MINOR_VERSION);
if (minorVersion != null) {
vendorNameAndVersion += minorVersion;
}
vendorName = (String) getProperties().get(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME);
minorVersion = (String) getProperties().get(PersistenceUnitProperties.SCHEMA_DATABASE_MINOR_VERSION);
majorVersion = (String) getProperties().get(PersistenceUnitProperties.SCHEMA_DATABASE_MAJOR_VERSION);
} else {
Connection conn = null;
try {
conn = (Connection) getReadLogin().connectToDatasource(null, this);
DatabaseMetaData dmd = conn.getMetaData();
vendorNameAndVersion = dmd.getDatabaseProductName() + dmd.getDatabaseMajorVersion() + dmd.getDatabaseProductVersion();
vendorName = dmd.getDatabaseProductName();
minorVersion = dmd.getDatabaseProductVersion();
majorVersion = Integer.toString(dmd.getDatabaseMajorVersion());
driverName = conn.getMetaData().getDriverName();
} catch (SQLException ex) {
if (throwException) {
Expand Down Expand Up @@ -268,15 +262,15 @@ protected void setOrDetectDatasource(boolean throwException) {
// null out the cached platform because the platform on the login
// will be changed by the following line of code
this.platform = null;
platformName = DBPlatformHelper.getDBPlatform(vendorNameAndVersion, getSessionLog());
platformName = DBPlatformHelper.getDBPlatform(vendorName, minorVersion, majorVersion, getSessionLog());
getLogin().setPlatformClassName(platformName);
} catch (EclipseLinkException classNotFound) {
if (platformName != null && platformName.indexOf("Oracle") != -1) {
try {
// If we are running against Oracle, it is possible that we are
// running in an environment where the extension OracleXPlatform classes can
// not be loaded. Try using the core OracleXPlatform classes
platformName = DBPlatformHelper.getDBPlatform("core."+ vendorNameAndVersion, getSessionLog());
platformName = DBPlatformHelper.getDBPlatform("core."+ vendorName, minorVersion, majorVersion, getSessionLog());
getLogin().setPlatformClassName(platformName);
} catch (EclipseLinkException oracleClassNotFound) {
// If we still cannot classload a matching OracleXPlatform class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2015, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015 IBM Corporation. All rights reserved.
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -26,8 +26,6 @@
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
import org.eclipse.persistence.logging.DefaultSessionLog;
import org.eclipse.persistence.platform.database.DB2MainframePlatform;
import org.eclipse.persistence.platform.database.DB2ZPlatform;
import org.eclipse.persistence.platform.database.DatabasePlatform;
import org.eclipse.persistence.sessions.DatabaseSession;
import org.junit.Assert;
Expand All @@ -51,19 +49,49 @@ public void test() {
@Test
public void testDB2ZOS() {
//Returned from jcc driver
Assert.assertEquals(DB2ZPlatform.class.getName(), getPlatformClass("DB2", "DSN10015"));
Assert.assertEquals(org.eclipse.persistence.platform.database.DB2ZPlatform.class.getName(), getPlatformClass("DB2", "10", "DSN10015"));
}

@Test
public void testDB2I() {
//Returned from jcc driver (DRDA)
Assert.assertEquals(DB2MainframePlatform.class.getName(), getPlatformClass("AS", "QSQ07020"));
Assert.assertEquals(org.eclipse.persistence.platform.database.DB2MainframePlatform.class.getName(), getPlatformClass("AS", "10", "QSQ07020"));

//Returned from type 2 native driver & type 4 open source driver (non-DRDA)
Assert.assertEquals(DB2MainframePlatform.class.getName(), getPlatformClass("DB2 UDB for AS/400", "07.02.0000 V7R2m0"));
Assert.assertEquals(org.eclipse.persistence.platform.database.DB2MainframePlatform.class.getName(), getPlatformClass("DB2 UDB for AS/400", "07.02.0000", "V7R2m0"));
}

private String getPlatformClass(String productName, String productVersion){
return DBPlatformHelper.getDBPlatform(productName + productVersion, log);
@Test
public void testMySQL() {
//Returned from jcc driver (DRDA)
Assert.assertEquals(org.eclipse.persistence.platform.database.MySQLPlatform.class.getName(), getPlatformClass("MySQL", "5", "5.5.5-10.1.33-MariaDB"));
}

@Test
public void testDerby() {
//Returned from jcc driver (DRDA)
Assert.assertEquals(org.eclipse.persistence.platform.database.JavaDBPlatform.class.getName(), getPlatformClass("Apache Derby", "10", "10.12.1.1 - (1704137)"));
}

@Test
public void testOracle() {
//Returned from jcc driver (DRDA)
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.OraclePlatform", getPlatformClass("Oracle", "7", "Oracle Database 7c"));
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.OraclePlatform", getPlatformClass("Oracle", "8", "Oracle Database 8c"));
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.Oracle9Platform", getPlatformClass("Oracle", "9", "Oracle Database 9c"));
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.Oracle10Platform", getPlatformClass("Oracle", "10", "Oracle Database 10c"));
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.Oracle11Platform", getPlatformClass("Oracle", "11", "Oracle Database 11c"));
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.Oracle12Platform", getPlatformClass("Oracle", "12", "Oracle Database 12c"));
Assert.assertEquals("org.eclipse.persistence.platform.database.oracle.Oracle18Platform", getPlatformClass("Oracle", "18", "Oracle Database 18c"));
}

@Test
public void testHanaDB() {
//Returned from jcc driver (DRDA)
Assert.assertEquals(org.eclipse.persistence.platform.database.HANAPlatform.class.getName(), getPlatformClass("HDB", "2", "2.00.040.00.1553674765"));
}

private String getPlatformClass(String productName, String minorVersion, String majorVersion){
return DBPlatformHelper.getDBPlatform(productName, minorVersion, majorVersion, log);
}
}

0 comments on commit 2637c10

Please sign in to comment.