Bug #100294 Connector/J produces NPE when saving a Date with prepared statements
Submitted: 22 Jul 2020 15:52 Modified: 9 Sep 2020 10:30
Reporter: Björn Voigt (OCA) Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:8.0.21 OS:Any
Assigned to: CPU Architecture:Any

[22 Jul 2020 15:52] Björn Voigt
Description:
Under some circumstances MySQL Connector/J 8.0.21 produces a NullPointerException when saving a Date value within a prepared statement.

The cause can be found in the clone() function of ServerPreparedQueryBindValue. This function does not copy the properties cacheDefaultTimezone and defaultTimezone.

    @Override
    public ServerPreparedQueryBindValue clone() {
        return new ServerPreparedQueryBindValue(this);
    }

    private ServerPreparedQueryBindValue(ServerPreparedQueryBindValue copyMe) {
        super(copyMe);

        this.serverTimeZone = copyMe.serverTimeZone;
        this.bufferType = copyMe.bufferType;
        this.calendar = copyMe.calendar;
        this.charEncoding = copyMe.charEncoding;
    }

When saving a Date value with function storeDate() this.cacheDefaultTimezone is null and this.cacheDefaultTimezone.getValue() produces a NullPointerFunction.

How to repeat:
1. Use mysql-connector-java-8.0.21.jar
2. Use the connection option useServerPrepStmts=true
3. Write a test program which stores a Date value with a prepared statement
4. It may be necessary to use Hibernate to repeat the error
5. It may be necessary to use a SQL batch

Suggested fix:
See attached patch
[22 Jul 2020 15:55] Björn Voigt
The patch ensures, that cacheDefaultTimezone and defaultTimeZone are copied

Attachment: clone-cacheDefaultTimezone-and-defaultTimeZone.patch (text/x-patch), 585 bytes.

[22 Jul 2020 21:14] Björn Voigt
This demo shows the bug with Connector/J 8.0.20 and 8.0.21 (works with 8.0.19)

Attachment: ConnectorJBug100294.java (text/x-java), 1.74 KiB.

[22 Jul 2020 21:15] Björn Voigt
The stracktrace for ConnectorJBug100294.java

Attachment: ConnectorJBug100294-stacktrace.log (text/x-log), 2.42 KiB.

[23 Jul 2020 6:01] MySQL Verification Team
Hello Björn,

Thank you for the report and test case.
Verified as described.

regards,
Umesh
[23 Jul 2020 6:03] MySQL Verification Team
Please note that in order to submit contributions you must first sign the Oracle Contribution Agreement (OCA). More details are described in "Contributions" tab, please ensure to re-send the patch via that tab. Otherwise we would not be able to accept it.

regards,
Umesh
[3 Aug 2020 19:33] Björn Voigt
The patch ensures, that cacheDefaultTimezone and defaultTimeZone are copied

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: clone-cacheDefaultTimezone-and-defaultTimeZone.patch (text/x-patch), 585 bytes.

[3 Aug 2020 19:34] Björn Voigt
This demo shows the bug with Connector/J 8.0.20 and 8.0.21 (works with 8.0.19)

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: ConnectorJBug100294.java (text/x-java), 1.74 KiB.

[20 Aug 2020 22:44] OCA Admin
Contribution submitted via Github - Fix for Bug #100294 
(*) Contribution by Björn Voigt (Github bjoernv, mysql-connector-j/pull/53#issuecomment-677935479): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_471199920.txt (text/plain), 1.24 KiB.

[9 Sep 2020 10:30] Alexander Soklakov
Closed as a duplicate of Bug#99713.
[9 Sep 2020 10:34] Alexander Soklakov
Hi Björn,

Thanks for the contribution, but we already pushed our own fix, though it's almost the same.