Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 6 Source backward-compatibility and SQL

My understanding is that in order to maintain source-compatibility, Java never introduces new methods to public interfaces, as that breaks existing clients implementing the interfaces. Java Release notes states

In general, the policy is as follows, except for any incompatibilities listed further below:

  • Maintenance releases (such as 1.4.1, 1.4.2) do not introduce any new language features or APIs. They will maintain source-compatibility with each other.

  • Functionality releases and major releases (such as 1.3.0, 1.4.0, 5.0) maintain upwards but not downwards source-compatibility.

Yet, the packages java.sql and javax.sql continue to evolve and introduce many incompatible changes. For example, I noticed the following incompatible changes (introduced in Java 6):

  • java.sql.Statement extends java.sql.Wrapper, requiring new two new methods.
  • java.sql.Statement introduces 3 new methods
  • java.sql.PreparedStatement introduces 19 new methods!
  • java.sql.ResultSet introduces 48 new methods!

Do you know how and why these methods got added? Is java.sql being treated differently from the rest of the platform? Do you know of the discussion/JSR around these additions?

like image 933
notnoop Avatar asked Aug 11 '09 16:08

notnoop


People also ask

Does Java support backward compatibility?

Backward or downward compatibility in Java API is a property of an API that allows older API usages to function without breaking their existing implementation when an API is modified.

Are newer Java versions backwards compatible?

Backward CompatibilityJava versions are expected to be binary backwards-compatible. For example, JDK 8 can run code compiled by JDK 7 or JDK 6. It is common to see applications leverage this backwards compatibility by using components built by different Java version.

Is Java 17 backwards compatible?

In general Java is extremely backward compatible. There have been a few minor breaking changes from JDK 8 to JDK 17, but the worst ones have had command-line options to disable them.

Is Java 8 forward compatible?

Java 7 is forward compatible with Java 8. Java 8 can be compiled so that it runs on a Java 7 VM (with -source 7 -target 7), but you can't use any of the newer APIs.


3 Answers

I got the following reply from a Sun Developer

The general evolution policy for APIs in the JDK for feature releases like JDK 7 is

  1. Don't break binary compatibility (as defined in JLSv3 chapter 13)
  2. Avoid introducing source incompatibilities
  3. Manage behavioral compatibility change

(For more, much more than you'd like to read on different kinds of compatibility see

"Kinds of Compatibility: Source, Binary, and Behavioral" and "Compatibly Evolving BigDecimal"

Adding methods to interfaces is binary compatible but source incompatible, so it is not commonly done. Generally, the more widely implemented an interface is, the less likely we are to add methods to it. The JDBC area is an exception to this policy and uses looser upgrade rules, but that does cause real issues when people want to upgrade to a new JDK release.

like image 189
notnoop Avatar answered Nov 05 '22 16:11

notnoop


Note that adding new methods only break source compatibility, already compiled implementations of Statement or ResultSet in a JDBC driver will continue to run on a newer JDK. Only when you try to call a new method you will get a NoSuchMethodError.

like image 45
Jörn Horstmann Avatar answered Nov 05 '22 15:11

Jörn Horstmann


They probably assume that database driver vendors that implement those methods are keeping up-to-date with new Java runtimes, and that it's better to introduce useful new methods and temporarily break compatibility.

Of course, they could've designed it better so that breaking compatibility wouldn't be necessary…

like image 43
John Calsbeek Avatar answered Nov 05 '22 17:11

John Calsbeek