Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Violation: Sql2o assigns object to field of wrong type without warning; crashes JVM #281

Open
DanStout opened this issue Jun 8, 2017 · 3 comments

Comments

@DanStout
Copy link

DanStout commented Jun 8, 2017

My PostgreSQL DB table has a field called "committed_at of type "DATE", which I map to a POJO with a field called committedAt of type "LocalDate". I forgot to add the custom converter for this, but I was expecting a warning about not being able to assign the field. Instead, it silently assigned the "java.sql.Date" object to a field of type "java.time.LocalDate", meaning attempts to call methods on it will crash the JVM.

Here is my mostly self-contained example:

public class Main {
    public static class Crime {
        public LocalDate committedAt;
        public Double latitude;
        public Double longitude;
        public String location;
        public String type;
    }

    public static void main(String[] args) {
        PGSimpleDataSource d = new PGSimpleDataSource();
        d.setUser("postgres");
        d.setPassword("password");
        d.setServerName("localhost");
        d.setPortNumber(5432);
        d.setDatabaseName("crime_db");

        Sql2o sql2o = new Sql2o(d);

        String sql = "SELECT committed_at committedAt, coordinates[0] latitude, coordinates[1] longitude, location, type FROM crimes";
        try(Connection con = sql2o.open()) {
            Crime crime = con.createQuery(sql).executeAndFetchFirst(Crime.class);
            LocalDate date = crime.committedAt;
            System.out.println(date.getClass().getName());
            System.out.println(date.getYear());
        }
    }
}

This outputs:

Java.sql.Date
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000638331f3, pid=15832, tid=0x00000000000004b4
#
# JRE version: Java(TM) SE Runtime Environment (8.0_131-b11) (build 1.8.0_131-b11)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# V  [jvm.dll+0x2031f3]

(The rest of the error is omitted)

Relevant build.gradle stuff:

    compile "org.postgresql:postgresql:42.1.1"
    compile "org.sql2o:sql2o:1.6.0-RC3"

Edit: This seems like it might be related to issue #206

@DanStout DanStout changed the title Access Violation: Sql2o sets field to object of wrong type without warning; crashes JVM Access Violation: Sql2o assigns object of wrong type to field without warning; crashes JVM Jun 8, 2017
@DanStout DanStout changed the title Access Violation: Sql2o assigns object of wrong type to field without warning; crashes JVM Access Violation: Sql2o assigns object to field of wrong type without warning; crashes JVM Jun 8, 2017
@GREX-USS
Copy link

GREX-USS commented Oct 2, 2017

This looks to be related to the change to the setter functionality to use sun.misc.Unsafe commit. Have you tried to use version 1.5.4 or Jodas Localdate with your existing version?

@DanStout
Copy link
Author

DanStout commented Oct 4, 2017

It sounds very possible that it was caused by the use of Unsafe. I don't have any interest in using Joda's version of LocalDate, and that isn't really the issue - the issue is that this crashed the JVM instead of throwing a runtime exception when it attempted to map a field to an unknown type.

@asolntsev
Copy link

@DanStout did you find any solution?
I created PR #352 that should fix this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants