package org.jdbc.adapter.version;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;

/**
 * Checks if the connection implements some selected methods and returns the JDBC spec version.
 * TODO: find better approach.
 *
 * @author David Matějček
 */
public enum JDBCVersion {
  /** JDBC 4.2 */
  JDBC4_2,
  /** JDBC 4.0 */
  JDBC4,
  /** JDBC 3.0 */
  JDBC3;

  /**
   * If the connection implements isWrapperFor, it probably implements JDBC4.
   *
   * @param connection
   * @return {@link JDBCVersion}
   */
  public static JDBCVersion getVersion(final Connection connection) {

    try {
      connection.getNetworkTimeout();
      return JDBC4_2;
    } catch (final SQLFeatureNotSupportedException e) {
      // okay, driver does not support this feature, but still implements JDBC 4.1.
      return JDBC4_2;
    } catch (final SQLException | AbstractMethodError e) {
      // nothing
    }

    try {
      // Glassfish's ConnectionHolder40 and ConnectionWrapper40 will success with
      // wrappers, but not with getClientInfo.
      connection.getClientInfo();
      connection.isWrapperFor(Connection.class);
      connection.unwrap(Connection.class);
      return JDBC4;
    } catch (final SQLFeatureNotSupportedException e) {
      return JDBC4;
    } catch (final SQLException | AbstractMethodError e) {
      return JDBC3;
    }
  }

}

@@ -60,38 +60,8 @@
 on;%0A
-import java.sql.SQLException;%0A
 impo
@@ -105,16 +105,16 @@
 eption;%0A
+
 %0A/**%0A * 
@@ -845,35 +845,32 @@
  %7D catch (final 
-SQL
 Exception %7C Abst
@@ -886,24 +886,198 @@
 dError e) %7B%0A
+      // WARNING: Some exceptions may be wrapped by reflection (Glassfish/Payara bug).%0A      // Solution should be similar as in: https://github.com/payara/Payara/issues/216%0A
       // not
@@ -1468,19 +1468,16 @@
  (final 
-SQL
 Exceptio
@@ -1501,24 +1501,198 @@
 dError e) %7B%0A
+      // WARNING: Some exceptions may be wrapped by reflection (Glassfish/Payara bug).%0A      // Solution should be similar as in: https://github.com/payara/Payara/issues/216%0A
       return

Fix for issue similar to Payara 216.