From 29213af88a72320674513a118c5c8692a4d1246c Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Tue, 9 Aug 2011 19:17:07 +0000 Subject: [PATCH] OPENJPA-1803: Fix EM.unwrap(java.sql.Connection.class) to account for a non-jdbc(sql) backed store. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1155500 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/persistence/EntityManagerImpl.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java index 17d8d96dd..2d2a3a34a 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java @@ -1660,6 +1660,7 @@ public class EntityManagerImpl * @exception if the given class is null, generic Object.class or a class * that is not wrapped by this receiver. */ + @SuppressWarnings("unchecked") public T unwrap(Class cls) { if (cls != null && cls != Object.class) { Object[] delegates = new Object[] { _broker.getInnermostDelegate(), _broker.getDelegate(), _broker, this }; @@ -1669,7 +1670,13 @@ public class EntityManagerImpl } // Only call getConnection() once we are certain that is the type that we need to unwrap. if (cls.isAssignableFrom(Connection.class)) { - return (T) getConnection(); + Object o = getConnection(); + if(Connection.class.isInstance(o)){ + return (T) o; + }else{ + // Try and cleanup if aren't going to return the connection back to the caller. + ImplHelper.close(o); + } } } // Set this transaction to rollback only (as per spec) here because the raised exception