Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.1
    • Fix Version/s: 2.2.0
    • Component/s: competitive, jpa
    • Labels:
      None

      Description

      This is currently the recommended way to access the javax.sql.Connection underlying an EntityManager:

      OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
      Connection con = (Connection) oem.getConnection();

      It would be more convenient to simply write

      Connection con = em.unwrap(Connection.class);

      That way, the client needing to access the Connection only depends on the javax.persistence API and not on org.apache.openjpa.*.

      I believe the JPA 2.0 spec neither mandates nor forbids this functionality. It is supported by Eclipselink 2.x.

      1. OPENJPA-1803.patch
        2 kB
        Sebastian Laskawiec

        Activity

        Harald Wellmann created issue -
        Donald Woods made changes -
        Field Original Value New Value
        Component/s competitive [ 12313757 ]
        Hide
        Sebastian Laskawiec added a comment -

        Hi, I also think it is a good idea. I think it is OK with the spec to return here java.sql.Connection:

        /**

        • Return an object of the specified type to allow access to the
        • provider-specific API. If the provider's EntityManager
        • implementation does not support the specified class, the
        • PersistenceException is thrown.

        I found also part of the documentation, which describes, how to obtain JDBC connection: http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/ref_guide_dbsetup_sqlconn.html

        Please take a look at this patch, I added _broker.getConnection() to EntityManagerImpl.unwrap method and added one test.

        Show
        Sebastian Laskawiec added a comment - Hi, I also think it is a good idea. I think it is OK with the spec to return here java.sql.Connection: /** Return an object of the specified type to allow access to the provider-specific API. If the provider's EntityManager implementation does not support the specified class, the PersistenceException is thrown. … I found also part of the documentation, which describes, how to obtain JDBC connection: http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/ref_guide_dbsetup_sqlconn.html Please take a look at this patch, I added _broker.getConnection() to EntityManagerImpl.unwrap method and added one test.
        Sebastian Laskawiec made changes -
        Attachment OPENJPA-1803.patch [ 12483291 ]
        Rick Curtis made changes -
        Assignee Rick Curtis [ curtisr7 ]
        Rick Curtis made changes -
        Fix Version/s 2.2.0 [ 12315910 ]
        Hide
        Rick Curtis added a comment -

        Committed code changes, test case, and doc to trunk.

        Sebastian – I modified your code patch slightly because I only want to call _broker.getConnection() when I am certain that a connection is really needed.

        Thanks for the patch!

        Show
        Rick Curtis added a comment - Committed code changes, test case, and doc to trunk. Sebastian – I modified your code patch slightly because I only want to call _broker.getConnection() when I am certain that a connection is really needed. Thanks for the patch!
        Rick Curtis made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Hide
        Pinaki Poddar added a comment -

        + if (cls.isAssignableFrom(Connection.class))

        { + return (T) getConnection(); + }

        This leaves the code vulnerable. The code should check that the return value of this.getConnection() is indeed a java.sql.Connection.
        OpenJPA EntityManager can operate at this level with datastore connections that are not java.sql.Connection.

        Show
        Pinaki Poddar added a comment - + if (cls.isAssignableFrom(Connection.class)) { + return (T) getConnection(); + } This leaves the code vulnerable. The code should check that the return value of this.getConnection() is indeed a java.sql.Connection. OpenJPA EntityManager can operate at this level with datastore connections that are not java.sql.Connection.
        Hide
        Rick Curtis added a comment -

        @Pinaki – I addressed your concerns with rev 1155500.

        Nice catch.

        Show
        Rick Curtis added a comment - @Pinaki – I addressed your concerns with rev 1155500. Nice catch.
        Hide
        Albert Lee added a comment -

        Close issue in preparation for 2.2.0 release.

        Show
        Albert Lee added a comment - Close issue in preparation for 2.2.0 release.
        Albert Lee made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Rick Curtis
            Reporter:
            Harald Wellmann
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development