You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2011/01/14 16:41:55 UTC

svn commit: r1059047 - in /openjpa/branches/2.1.x: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ openjpa-persistence/src/main/resources/org/apache/openjpa/pe...

Author: mikedd
Date: Fri Jan 14 15:41:55 2011
New Revision: 1059047

URL: http://svn.apache.org/viewvc?rev=1059047&view=rev
Log:
OPENJPA-1906: Add warning for ConnectionRetainMode=always

Added:
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/TestConnectionRetainModeWarning.java   (with props)
Modified:
    openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
    openjpa/branches/2.1.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/TestConnectionRetainModeWarning.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/TestConnectionRetainModeWarning.java?rev=1059047&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/TestConnectionRetainModeWarning.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/TestConnectionRetainModeWarning.java Fri Jan 14 15:41:55 2011
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.log;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.openjpa.lib.log.AbstractLog;
+import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.lib.log.LogFactory;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.persistence.PersistenceProviderImpl;
+import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
+
+public class TestConnectionRetainModeWarning extends AbstractPersistenceTestCase implements LogFactory {
+    private static List<String> messages = new ArrayList<String>();
+
+    Localizer _loc = Localizer.forPackage(PersistenceProviderImpl.class);
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+        messages.clear();
+    }
+
+    // Start LogFactory implementation
+    public Log getLog(String channel) {
+        return new AbstractLog() {
+
+            protected boolean isEnabled(short logLevel) {
+                return true;
+            }
+
+            @Override
+            public void trace(Object message) {
+                messages.add(message.toString());
+            }
+
+            protected void log(short type, String message, Throwable t) {
+                messages.add(message);
+            }
+
+            @Override
+            public void error(Object message) {
+                messages.add(message.toString());
+            }
+
+            @Override
+            public void warn(Object message) {
+                super.warn(message.toString());
+            }
+
+            @Override
+            public void info(Object message) {
+                messages.add(message.toString());
+            }
+        };
+    }
+
+    public void assertMessageContains(String s) {
+        for (String message : messages) {
+            if (message.contains(s)) {
+                return;
+            }
+        }
+        fail("Did not find message " + s + " in " + messages);
+    }
+
+    public void assertMessageNotFound(String s) {
+        for (String message : messages) {
+            if (message.contains(s)) {
+                fail("Found unexpected messsage " + s);
+            }
+        }
+    }
+
+    public void testInfoMessage() {
+        EntityManagerFactory emf =
+            createEMF("openjpa.Log", this.getClass().getCanonicalName(), "openjpa.ConnectionRetainMode", "always");
+
+        assertNotNull(emf);
+        assertMessageContains(_loc.get("retain-always", getPersistenceUnitName()).toString());
+        emf.close();
+    }
+
+    public void testInfoMessageNotFound() {
+        EntityManagerFactory emf =
+            createEMF("openjpa.Log", this.getClass().getCanonicalName(), "openjpa.ConnectionRetainMode", "on-demand");
+
+        assertNotNull(emf);
+        assertMessageNotFound(_loc.get("retain-always", getPersistenceUnitName()).toString());
+        emf.close();
+    }
+
+}

Propchange: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/log/TestConnectionRetainModeWarning.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java?rev=1059047&r1=1059046&r2=1059047&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java (original)
+++ openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java Fri Jan 14 15:41:55 2011
@@ -40,6 +40,7 @@ import org.apache.openjpa.enhance.PCEnha
 import org.apache.openjpa.kernel.AbstractBrokerFactory;
 import org.apache.openjpa.kernel.Bootstrap;
 import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.kernel.ConnectionRetainModes;
 import org.apache.openjpa.lib.conf.Configuration;
 import org.apache.openjpa.lib.conf.ConfigurationProvider;
 import org.apache.openjpa.lib.conf.Configurations;
@@ -102,6 +103,11 @@ public class PersistenceProviderImpl
             // Create appropriate LifecycleEventManager
             loadValidator(factory);
             
+            if (conf.getConnectionRetainModeConstant() == ConnectionRetainModes.CONN_RETAIN_ALWAYS) {
+                // warn about EMs holding on to connections.
+                _log.warn(_loc.get("retain-always", conf.getId()));
+            }
+            
             OpenJPAEntityManagerFactory emf = JPAFacadeHelper.toEntityManagerFactory(factory);
             if (_log.isTraceEnabled()) {
                 _log.trace(this + " creating " + emf + " for PU " + name + ".");
@@ -196,6 +202,11 @@ public class PersistenceProviderImpl
                     _log.warn(_loc.get("transformer-registration-error", pui));
                 }
             }
+            
+            if (conf.getConnectionRetainModeConstant() == ConnectionRetainModes.CONN_RETAIN_ALWAYS) {
+                // warn about container managed EMs holding on to connections.
+                _log.warn(_loc.get("cm-retain-always",conf.getId()));
+            }
 
             // Create appropriate LifecycleEventManager
             loadValidator(factory);

Modified: openjpa/branches/2.1.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties?rev=1059047&r1=1059046&r2=1059047&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties (original)
+++ openjpa/branches/2.1.x/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties Fri Jan 14 15:41:55 2011
@@ -244,3 +244,11 @@ invalid-cfname-prop: The "{0}" configura
 invalid-persistence-property: The property "{0}={1}" was detected while loading configuration. However, it is invalid \
 and cannot be configured at the provider level, so it is ignored. Please consult the documentation for the correct \
 usage of this property.
+retain-always: The persistence property 'openjpa.ConnectionRetainMode' is set to 'always' for PersistenceUnit \
+"{0}". Each EntityManager created for this PersistenceUnit will open a single connection. The connection will not be \
+released until the EntityManager is closed. 
+cm-retain-always: The persistence property 'openjpa.ConnectionRetainMode' is set to 'always' for the container \
+managed PersistenceUnit "{0}". Each EntityManager created for this PersistenceUnit will open a single connection. The \
+connection will not be released until the EntityManager is closed. If the application uses container managed \
+EntityManagers this property should not be used because these EntityManagers may remain open for extended periods of \
+time.