Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z5KVc-0002kI-O3 for pgsql-jdbc@arkaria.postgresql.org; Wed, 17 Jun 2015 21:04:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z5KVc-0004CT-8X for pgsql-jdbc@arkaria.postgresql.org; Wed, 17 Jun 2015 21:04:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Z5KVb-0004CE-B0 for pgsql-jdbc@postgresql.org; Wed, 17 Jun 2015 21:04:31 +0000 Received: from smtprelay02.ispgateway.de ([80.67.31.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z5KVW-0002AW-Pj for pgsql-jdbc@postgresql.org; Wed, 17 Jun 2015 21:04:30 +0000 Received: from [88.66.14.101] (helo=Zauselmaus) by smtprelay02.ispgateway.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84) (envelope-from ) id 1Z5KVV-0000rq-IZ for pgsql-jdbc@postgresql.org; Wed, 17 Jun 2015 23:04:25 +0200 From: "Markus KARG" To: "'List'" References: <000c01d0a7b6$97571ee0$c6055ca0$@eu> <005101d0a86a$f18ffe90$d4affbb0$@eu> <60b20bb834cf54db8232239b9c27fcc2@imap.procolix.com> In-Reply-To: Subject: Re: Pre-processing during build Date: Wed, 17 Jun 2015 23:04:33 +0200 Organization: Markus KARG, Staatl. gepr. Inf. Message-ID: <007201d0a941$35e087a0$a1a196e0$@eu> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0073_01D0A951.F96957A0" X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AdCo+Me6ADwYA//aSHCR3C5jcUlnqgASGB3g Content-Language: de X-AVK-Virus-Check: AVA 25.1869;71FD75 X-AVK-Spam-Check: 1; str=0001.0A0C0201.5581E0D9.010C,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0; F1206 X-Df-Sender: bWFya3VzQGhlYWRjcmFzaGluZy5ldQ== X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. ------=_NextPart_000_0073_01D0A951.F96957A0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable +1 good posting =20 From: pgsql-jdbc-owner@postgresql.org = [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Christopher BROWN Sent: Mittwoch, 17. Juni 2015 14:24 To: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Pre-processing during build =20 As has been said by Markus KARG and others, you CAN produce a driver = using bytecode for Java-6 / JDBC-4.1 including Java-8 / JDBC-4.2 (and = Java-7 / JDBC 4.1) types and method signatures. I've used this = technique in many production applications, and have tried out this = specific case just now, as a check. =20 So, for example, if you use "javac" from Java-8 and even include the = Java-8 (JDBC-4.2) definition of PreparedStatement, compiling with = source=3D1.6 and target=3D1.6 options, your JDBC-4.2 implementation of = PreparedStatement WILL load into Java-6 with JDBC-4. You can even = annotate your implementations with @Override, no problem. You will NOT = have a problem with clients that expect a Java-6 / JDBC-4 API because = there is no way you can compile such a client to invoke a JDBC-4.2 = method (it would be a compiler error). Java only tries to resolve = classes on-demand, that is when it runs a code branch in a method body = that refers to a type or invokes a method with such a type as part of = its signature, and NOT when loading or instantiating your class. If you = never call it, you'll never have a problem. =20 You WILL have problems however in the following (avoidable) cases: =20 - if your implementation of a JDBC-4 driver calls code that in turn = refers to types, fields, or methods that depend on a more recent method = of the Java API, for example : - static initialization - constructor calls to code that depends on a more recent API version - an implementation of a JDBC-4 method that calls a JDBC-4.1 or -4.2 = method (typically method overloading with the noble intention of = avoiding copying-and-pasting code) - as has been suggested, the safest workaround is to just use = "extends" where appropriate, instance of generating code from templates - use of reflection (or proxies) to examine classes or invoke methods - use of BeansIntrospector =20 The problem is not in compiling, it's about ensuring that once client = code invokes a JDBC-4 method, that your implementation of that method = doesn't call in turn any code that it shouldn't. =20 Code coverage metrics are an additional guarantee but you'd have to be = very sure you've got correct coverage for all version-dependant code = paths. Compiler constraints are probably safer ; I'll discuss that in a = moment. =20 First, a few remarks concerning some of the previous posts : =20 - = https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html= #setObject-int-java.lang.Object-java.sql.SQLType- is actually = implemented as a Java-8 "default" method. You don't need to implement = it directly in the driver. =20 - https://gist.github.com/vlsi/aeeb4a61d9c2b67ad213 is a doomed-to-fail = example, not due to bytecode versions but because Java uses reflection = (see above list of problems) to find your "main" method, and so trips up = on the method using Java-8 types. Restructured as follows (two classes = with separate source files), it works: =20 ----8<---- Jre7Test.java ----8<---- =20 public class Jre7Test { public static void main(String args[]) { System.out.println(Jre7TestCompanion.greeting()); } } =20 ----8<---- Jre7TestCompanion.java ----8<---- =20 import java.time.Duration; import java.util.Optional; =20 public class Jre7TestCompanion { public static Optional optional(java.time.Duration = duration) { return Optional.of(duration); } =20 public static String greeting() { return "Hello, world"; } } =20 ----8<--------8<---- =20 (the above compiled and run with the exact same commands on Mac OS X = too). =20 The safest way is to use incremental compilation (all integrated into a = single automated build, with no preference for build tool). Using = fictional package and class names to demonstrate the idea, here's how it = could be done. =20 For example, produce an intermediate "pgjdbc_4.0.jar" using a "JDBC-4" = package (1.6 API as bootstrap classpath for "javac", with 1.8 compiler = and source/target 1.6): =20 jdbc_4_0.PGDriver_4_0 jdbc_4_0.PGPreparedStatement_4_0 jdbc_4_0.PGResultSet_4_0 ...etc =20 Add the resulting "jar" to the classpath for the next step, with classes = that extend the above, producing "pgjdbc_4.1.jar" (useless unless = "pgjdbc_4.0.jar" is also in the classpath). (1.7 API as bootstrap = classpath for "javac", with 1.8 compiler and source/target 1.6) =20 jdbc_4_1.PGDriver_4_1 extends jdbc_4_0.PGDriver_4_0 jdbc_4_1.PGPreparedStatement_4_1 extends = jdbc_4_0.PGPreparedStatement_4_0 jdbc_4_1.PGResultSet_4_1 extends jdbc_4_0.PGResultSet_4_0 ...etc =20 Then add the both resulting "jar" to the classpath for the next step, = with classes that extend the above, producing "pgjdbc_4.2.jar" (useless = unless "pgjdbc_4.0.jar" is also in the classpath, along with = "pgjdbc_4.1.jar"). (1.8 API as bootstrap classpath for "javac", with 1.8 = compiler and source/target 1.6) =20 jdbc_4_2.PGDriver_4_2 extends jdbc_4_1.PGDriver_4_1 jdbc_4_2.PGPreparedStatement_4_2 extends = jdbc_4_1.PGPreparedStatement_4_1 jdbc_4_2.PGResultSet_4_2 extends jdbc_4_1.PGResultSet_4_1 ...etc =20 Then, merge all JARs into a single JAR. Clients could then refer to the = specific driver version they require in code, or use a generic Driver = class that (in the constructor) detects the appropriate JDBC version and = fixes a "final" int or Enum field, used thereafter in "switch" blocks to = call the appropriate driver version, acting as a lightweight proxy when = the specific driver version can't be referred to (for backwards = compatibility). More adventurous developers might even suggest usage of = method handles from Java 7 onwards to eliminate the negligeable overhead = of a switch statements, but I'd personally rely on the JVM to optimise = that away. Note that this is only necessary for the Driver = implementation, as no-one (apart from the driver implementors) should = ever call "new PreparedStatement" or whatever. =20 Hope that helps ; hope it's not redundant with regards to messages sent = since I started typing away my 2 cents... In any case, I regularly use = these techniques in production code with no accidents. =20 -- Christopher =20 =20 =20 On 17 June 2015 at 13:05, Sehrope Sarkuni wrote: On Wed, Jun 17, 2015 at 6:15 AM, Dave Cramer wrote: I'm not sure this is a great example as Optional itself is a java 8 = construct. =20 Either way Spring is able to do this, as are others? =20 The approach used by Spring won't work for the JDBC driver. The crux of = the issue is that the newest version of the JDBC spec include Java 8 = types in method signatures of public interfaces that exist in Java . = Spring doesn't do that. =20 The public interfaces and classes for the older JDK versions they = support (i.e. 6 or 7) only expose types that exist in those JDK = versions. For older classes they've added internal support for Java 8 = types that is dynamically checked, but it's done by wrapping the = integration in an inner class. Here's an example: = https://github.com/spring-projects/spring-framework/blob/f41de12cf62aebf1= be9b30be590c12eb2c030853/spring-beans/src/main/java/org/springframework/b= eans/AbstractNestablePropertyAccessor.java#L1041 =20 There's no way to make that work when a public interface exposes classes = that won't exist on the run time. It may have been possible with older = upgrades to the JDBC spec (ex: 4 to 4.1) as there weren't any JDK = 1.7-only classes used in methods signatures of existing public = interfaces. Compiling with an older bytecode target would allow and = older JDK to simply ignore those methods as they would not be part of = the public signature. =20 In JDBC 4.2 that's not true though. For example the JDBC 4.2 = PreparedStatement class has a new setObject(...) that uses a Java 8 only = class: =20 PreparedStatment.setObject(int parameterIndex, Object x, SQLType = targetSqlType): = https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html= #setObject-int-java.lang.Object-java.sql.SQLType- =20 SQLType (Java 8 only): = https://docs.oracle.com/javase/8/docs/api/java/sql/SQLType.html =20 That method signature can't appear in a driver that is going to be used = in JDK 6 or 7. There's no way to hide it internally as it's part of the = public signature. =20 We're going to need some kind of preprocessing step to handle things = like this. =20 Regards, -- Sehrope Sarkuni Founder & CEO | JackDB, Inc. | https://www.jackdb.com/ =20 =20 ------=_NextPart_000_0073_01D0A951.F96957A0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

+1 good posting

 

From:= = pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] = On Behalf Of Christopher BROWN
Sent: Mittwoch, 17. Juni = 2015 14:24
To: pgsql-jdbc@postgresql.org
Subject: = Re: [JDBC] Pre-processing during build

 

As = has been said by Markus KARG and others, you CAN produce a driver using = bytecode for Java-6 / JDBC-4.1 including Java-8 / JDBC-4.2 (and Java-7 / = JDBC 4.1) types and method signatures.  I've used this technique in = many production applications, and have tried out this specific case just = now, as a check.

 

So, for example, if you use "javac" from = Java-8 and even include the Java-8 (JDBC-4.2) definition of = PreparedStatement, compiling with source=3D1.6 and target=3D1.6 options, = your JDBC-4.2 implementation of PreparedStatement WILL load into Java-6 = with JDBC-4.  You can even annotate your implementations with = @Override, no problem.  You will NOT have a problem with clients = that expect a Java-6 / JDBC-4 API because there is no way you can = compile such a client to invoke a JDBC-4.2 method (it would be a = compiler error).  Java only tries to resolve classes on-demand, = that is when it runs a code branch in a method body that refers to a = type or invokes a method with such a type as part of its signature, and = NOT when loading or instantiating your class.  If you never call = it, you'll never have a problem.

 

You WILL have problems however in the following = (avoidable) cases:

 

- = if your implementation of a JDBC-4 driver calls code that in turn refers = to types, fields, or methods that depend on a more recent method of the = Java API, for example :

  - static = initialization

  - = constructor calls to code that depends on a more recent API = version

  - an = implementation of a JDBC-4 method that calls a JDBC-4.1 or -4.2 method = (typically method overloading with the noble intention of avoiding = copying-and-pasting code)

  - as has been suggested, the safest workaround = is to just use "extends" where appropriate, instance of = generating code from templates

- use of reflection (or proxies) to examine classes or = invoke methods

- use of = BeansIntrospector

 

The problem is not in compiling, it's about ensuring = that once client code invokes a JDBC-4 method, that your implementation = of that method doesn't call in turn any code that it = shouldn't.

 

Code coverage metrics are an additional guarantee but = you'd have to be very sure you've got correct coverage for all = version-dependant code paths.  Compiler constraints are probably = safer ; I'll discuss that in a moment.

 

First, a few remarks concerning some of the previous = posts :

 

- https://docs.o= racle.com/javase/8/docs/api/java/sql/PreparedStatement.html#setObject-int= -java.lang.Object-java.sql.SQLType- is actually implemented as a = Java-8 "default" method.  You don't need to implement it = directly in the driver.

 

- https://gist.g= ithub.com/vlsi/aeeb4a61d9c2b67ad213 is a doomed-to-fail example, not = due to bytecode versions but because Java uses reflection (see above = list of problems) to find your "main" method, and so trips up = on the method using Java-8 types.  Restructured as follows (two = classes with separate source files), it = works:

 

----8<---- Jre7Test.java = ----8<----

 

public class Jre7Test {

    public static void main(String args[]) = {

      =   = System.out.println(Jre7TestCompanion.greeting());

    }

}

 

----8<---- Jre7TestCompanion.java = ----8<----

 

import java.time.Duration;

import java.util.Optional;

 

public class Jre7TestCompanion = {

    public = static Optional<Duration> optional(java.time.Duration duration) = {

      =   return Optional.of(duration);

    }

 

    public static String greeting() = {

      =   return "Hello, world";

    }

}

 

----8<--------8<----

 

(the above compiled and run with the exact same = commands on Mac OS X too).

 

The safest way is to use incremental compilation (all = integrated into a single automated build, with no preference for build = tool).  Using fictional package and class names to demonstrate the = idea, here's how it could be done.

 

For example, produce an intermediate = "pgjdbc_4.0.jar" using a "JDBC-4" package (1.6 API = as bootstrap classpath for "javac", with 1.8 compiler and = source/target 1.6):

 

jdbc_4_0.PGDriver_4_0

jdbc_4_0.PGPreparedStatement_4_0

jdbc_4_0.PGResultSet_4_0

...etc

 

Add the resulting "jar" to the classpath for = the next step, with classes that extend the above, producing = "pgjdbc_4.1.jar" (useless unless "pgjdbc_4.0.jar" is = also in the classpath).  (1.7 API as bootstrap classpath for = "javac", with 1.8 compiler and source/target = 1.6)

 

jdbc_4_1.PGDriver_4_1 extends = jdbc_4_0.PGDriver_4_0

jdbc_4_1.PGPreparedStatement_4_1 extends = jdbc_4_0.PGPreparedStatement_4_0

jdbc_4_1.PGResultSet_4_1 extends = jdbc_4_0.PGResultSet_4_0

...etc

 

Then add the both resulting "jar" to the = classpath for the next step, with classes that extend the above, = producing "pgjdbc_4.2.jar" (useless unless = "pgjdbc_4.0.jar" is also in the classpath, along with = "pgjdbc_4.1.jar"). (1.8 API as bootstrap classpath for = "javac", with 1.8 compiler and source/target = 1.6)

 

jdbc_4_2.PGDriver_4_2 extends = jdbc_4_1.PGDriver_4_1

jdbc_4_2.PGPreparedStatement_4_2 extends = jdbc_4_1.PGPreparedStatement_4_1

jdbc_4_2.PGResultSet_4_2 extends = jdbc_4_1.PGResultSet_4_1

...etc

 

Then, merge all JARs into a single JAR.  Clients = could then refer to the specific driver version they require in code, or = use a generic Driver class that (in the constructor) detects the = appropriate JDBC version and fixes a "final" int or Enum = field, used thereafter in "switch" blocks to call the = appropriate driver version, acting as a lightweight proxy when the = specific driver version can't be referred to (for backwards = compatibility).  More adventurous developers might even suggest = usage of method handles from Java 7 onwards to eliminate the negligeable = overhead of a switch statements, but I'd personally rely on the JVM to = optimise that away. Note that this is only necessary for the Driver = implementation, as no-one (apart from the driver implementors) should = ever call "new PreparedStatement" or = whatever.

 

Hope that helps ; hope it's not redundant with regards = to messages sent since I started typing away my 2 cents...  In any = case, I regularly use these techniques in production code with no = accidents.

 

--
Christopher

 

 

 

On 17 = June 2015 at 13:05, Sehrope Sarkuni <sehrope@jackdb.com> = wrote:

On Wed, Jun 17, 2015 at 6:15 AM, Dave Cramer <pg@fastcrypt.com> = wrote:

I'm not sure this is a great example as Optional = itself is a java 8 construct.

 

Either way Spring is able to do this, as are = others?

 

The approach used by Spring won't work for the JDBC = driver. The crux of the issue is that the newest version of the JDBC = spec include Java 8 types in method signatures of public interfaces that = exist in Java . Spring doesn't do that.

 

The public interfaces and classes for the older JDK = versions they support (i.e. 6 or 7) only expose types that exist in = those JDK versions. For older classes they've added internal support for = Java 8 types that is dynamically checked, but it's done by wrapping the = integration in an inner class. Here's an example: https://github.com/spring-projects/spring-framework/blo= b/f41de12cf62aebf1be9b30be590c12eb2c030853/spring-beans/src/main/java/org= /springframework/beans/AbstractNestablePropertyAccessor.java#L1041

 

There's no way to make that work when a public = interface exposes classes that won't exist on the run time. It may have = been possible with older upgrades to the JDBC spec (ex: 4 to 4.1) as = there weren't any JDK 1.7-only classes used in methods signatures of = existing public interfaces. Compiling with an older bytecode target = would allow and older JDK to simply ignore those methods as they would = not be part of the public signature.

 

In JDBC 4.2 that's not true though. For example the = JDBC 4.2 PreparedStatement class has a new setObject(...) that uses a = Java 8 only class:

 

PreparedStatment.setObject(int parameterIndex, Object = x, SQLType targetSqlType): https://docs.oracle.com/javase/8/docs/api/java/sql/Prep= aredStatement.html#setObject-int-java.lang.Object-java.sql.SQLType-

 

 

That method signature can't appear in a driver that is = going to be used in JDK 6 or 7. There's no way to hide it internally as = it's part of the public signature.

 

We're going to need some kind of preprocessing step to = handle things like this.

 

Regards,

=

-- Sehrope = Sarkuni

Founder & CEO | JackDB, = Inc. | https://www.jackdb.com/

=

 

 

------=_NextPart_000_0073_01D0A951.F96957A0--