You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by da...@apache.org on 2007/03/15 03:38:06 UTC
svn commit: r518446 - in
/incubator/openejb/trunk/openejb3/container/openejb-core/src:
main/java/org/apache/openejb/resource/
main/java/org/apache/openejb/resource/jdbc/
test/java/org/apache/openejb/assembler/classic/
Author: dain
Date: Wed Mar 14 19:38:05 2007
New Revision: 518446
URL: http://svn.apache.org/viewvc?view=rev&rev=518446
Log:
Fixed pooling bug with unmanaged jdbc connections
Added:
incubator/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/JdbcConfigTest.java
Modified:
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/SharedLocalConnectionManager.java
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedConnectionFactory.java
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionFactory.java
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionRequestInfo.java
Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/SharedLocalConnectionManager.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/SharedLocalConnectionManager.java?view=diff&rev=518446&r1=518445&r2=518446
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/SharedLocalConnectionManager.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/SharedLocalConnectionManager.java Wed Mar 14 19:38:05 2007
@@ -37,8 +37,11 @@
import javax.transaction.TransactionManager;
import javax.transaction.SystemException;
import javax.transaction.RollbackException;
+import javax.transaction.Synchronization;
+import javax.transaction.Status;
import org.apache.openejb.resource.jdbc.JdbcManagedConnectionFactory;
+import org.apache.openejb.resource.jdbc.JdbcUnmanagedConnection;
/**
* @org.apache.xbean.XBean element="sharedLocalConnectionManager"
@@ -121,9 +124,9 @@
conn.getLocalTransaction().commit();
this.cleanup(conn);
}
- } catch (javax.transaction.SystemException se) {
+ } catch (SystemException se) {
- } catch (javax.resource.ResourceException re) {
+ } catch (ResourceException re) {
}
}
@@ -135,7 +138,7 @@
conn.destroy();
threadConnectionCache.get().removeConnection(conn);
- } catch (javax.resource.ResourceException re) {
+ } catch (ResourceException re) {
}
}
@@ -152,7 +155,9 @@
if (conn != null) {
try {
conn.cleanup();
- connSet.add(conn);
+ if (!(conn instanceof JdbcUnmanagedConnection)) {
+ connSet.add(conn);
+ }
} catch (ResourceException re) {
try {
conn.destroy();
@@ -167,7 +172,7 @@
public void localTransactionStarted(ConnectionEvent event) {
}
- static class Synchronizer implements javax.transaction.Synchronization {
+ static class Synchronizer implements Synchronization {
LocalTransaction localTx;
public Synchronizer(LocalTransaction lt) {
@@ -178,16 +183,16 @@
}
public void afterCompletion(int status) {
- if (status == javax.transaction.Status.STATUS_COMMITTED) {
+ if (status == Status.STATUS_COMMITTED) {
try {
localTx.commit();
- } catch (javax.resource.ResourceException re) {
+ } catch (ResourceException re) {
throw new RuntimeException("JDBC driver failed to commit transaction. " + re.getMessage());
}
} else {
try {
localTx.rollback();
- } catch (javax.resource.ResourceException re) {
+ } catch (ResourceException re) {
throw new RuntimeException("JDBC driver failed to rollback transaction. " + re.getMessage());
}
}
Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedConnectionFactory.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedConnectionFactory.java?view=diff&rev=518446&r1=518445&r2=518446
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedConnectionFactory.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedConnectionFactory.java Wed Mar 14 19:38:05 2007
@@ -21,16 +21,25 @@
import javax.resource.spi.EISSystemException;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ResourceAdapter;
+import javax.resource.spi.ManagedConnectionFactory;
+import javax.resource.ResourceException;
+import javax.resource.NotSupportedException;
import javax.security.auth.Subject;
import java.sql.DriverManager;
import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.Set;
+import java.io.PrintWriter;
+import java.io.Serializable;
-public class BasicManagedConnectionFactory implements javax.resource.spi.ManagedConnectionFactory, java.io.Serializable {
+public class BasicManagedConnectionFactory implements ManagedConnectionFactory, Serializable {
+ private static final long serialVersionUID = 4339732142876149245L;
private final String jdbcDriver;
private final String jdbcUrl;
private final String defaultUserName;
private final String defaultPassword;
- private java.io.PrintWriter logWriter;
+ private PrintWriter logWriter;
private final int hashCode;
private final JdbcManagedConnectionFactory managedConnectionFactory;
@@ -43,15 +52,15 @@
hashCode = jdbcDriver.hashCode() ^ jdbcUrl.hashCode() ^ defaultUserName.hashCode() ^ defaultPassword.hashCode();
}
- public Object createConnectionFactory() throws javax.resource.ResourceException {
- throw new javax.resource.NotSupportedException("This connector must be used with an application server connection manager");
+ public Object createConnectionFactory() throws ResourceException {
+ throw new NotSupportedException("This connector must be used with an application server connection manager");
}
- public Object createConnectionFactory(ConnectionManager cxManager) throws javax.resource.ResourceException {
+ public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException {
return new JdbcConnectionFactory(managedConnectionFactory, cxManager, jdbcUrl, jdbcDriver, defaultPassword, defaultUserName);
}
- public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) throws javax.resource.ResourceException {
+ public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) throws ResourceException {
try {
JdbcConnectionRequestInfo request = (JdbcConnectionRequestInfo) connectionRequestInfo;
Connection connection = DriverManager.getConnection(jdbcUrl, request.getUserName(), request.getPassword());
@@ -59,7 +68,7 @@
return new JdbcUnmanagedConnection(managedConnectionFactory, connection, request);
}
return new JdbcManagedConnection(managedConnectionFactory, connection, request);
- } catch (java.sql.SQLException e) {
+ } catch (SQLException e) {
throw (EISSystemException) new EISSystemException("Could not obtain a physical JDBC connection from the DriverManager").initCause(e);
}
}
@@ -72,7 +81,7 @@
return jdbcDriver.equals(that.jdbcDriver) && jdbcUrl.equals(that.jdbcUrl) && defaultUserName.equals(that.defaultUserName) && defaultPassword.equals(that.defaultPassword);
}
- public java.io.PrintWriter getLogWriter() {
+ public PrintWriter getLogWriter() {
return logWriter;
}
@@ -80,19 +89,20 @@
return hashCode;
}
- public ManagedConnection matchManagedConnections(java.util.Set connectionSet, javax.security.auth.Subject subject, ConnectionRequestInfo connectionInfo) throws javax.resource.ResourceException {
+ public ManagedConnection matchManagedConnections(Set connectionSet, Subject subject, ConnectionRequestInfo connectionInfo) throws ResourceException {
if (!(connectionInfo instanceof JdbcConnectionRequestInfo)) {
return null;
}
-
- JdbcManagedConnection[] connections = (JdbcManagedConnection[]) connectionSet.toArray(new JdbcManagedConnection[]{});
- int i = 0;
- for (; i < connections.length && !connections[i].getRequestInfo().equals(connectionInfo); i++) {
+ for (Iterator iterator = connectionSet.iterator(); iterator.hasNext();) {
+ JdbcManagedConnection connection = (JdbcManagedConnection) iterator.next();
+ if (connection.getRequestInfo().equals(connectionInfo)) {
+ return connection;
+ }
}
- return (i < connections.length) ? connections[i] : null;
+ return null;
}
- public void setLogWriter(java.io.PrintWriter out) {
+ public void setLogWriter(PrintWriter out) {
logWriter = out;
}
Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionFactory.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionFactory.java?view=diff&rev=518446&r1=518445&r2=518446
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionFactory.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionFactory.java Wed Mar 14 19:38:05 2007
@@ -18,19 +18,23 @@
import javax.naming.Reference;
import javax.resource.ResourceException;
+import javax.resource.Referenceable;
import javax.resource.spi.ApplicationServerInternalException;
import javax.resource.spi.ConnectionManager;
-import javax.resource.spi.ManagedConnectionFactory;
import javax.resource.spi.ResourceAdapterInternalException;
import javax.resource.spi.ResourceAllocationException;
+import javax.resource.spi.SecurityException;
+import javax.sql.DataSource;
import java.io.PrintWriter;
+import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
-public class JdbcConnectionFactory implements javax.sql.DataSource, javax.resource.Referenceable, java.io.Serializable {
+public class JdbcConnectionFactory implements DataSource, Referenceable, Serializable {
+ private static final long serialVersionUID = 7846198858025323860L;
private Reference jndiReference;
- private final transient ManagedConnectionFactory managedConnectionFactory;
+ private final transient JdbcManagedConnectionFactory managedConnectionFactory;
private final transient ConnectionManager connectionManager;
private final String jdbcUrl;
private final String jdbcDriver;
@@ -39,7 +43,7 @@
private transient PrintWriter logWriter;
private int logTimeout = 0;
- public JdbcConnectionFactory(ManagedConnectionFactory managedConnectionFactory,
+ public JdbcConnectionFactory(JdbcManagedConnectionFactory managedConnectionFactory,
ConnectionManager connectionManager, String jdbcUrl,
String jdbcDriver, String defaultPassword, String defaultUserName) throws ResourceException {
this.managedConnectionFactory = managedConnectionFactory;
@@ -79,8 +83,12 @@
return getConnection(defaultUserName, defaultPassword);
}
- public Connection getConnection(java.lang.String username, java.lang.String password) throws SQLException {
- return getConnection(new JdbcConnectionRequestInfo(username, password, jdbcDriver, jdbcUrl));
+ public Connection getConnection(String username, String password) throws SQLException {
+ JdbcConnectionRequestInfo requestInfo = new JdbcConnectionRequestInfo(username, password, jdbcDriver, jdbcUrl);
+ if (managedConnectionFactory.isUnmanaged()) {
+ requestInfo = new JdbcUnmanagedConnection.UnmanagedConnectionRequestInfo(requestInfo);
+ }
+ return getConnection(requestInfo);
}
protected Connection getConnection(JdbcConnectionRequestInfo connectionRequestInfo) throws SQLException {
@@ -89,7 +97,7 @@
return (Connection) connectionManager.allocateConnection(managedConnectionFactory, connectionRequestInfo);
} catch (ApplicationServerInternalException e) {
throw convertToSQLException(e, "Application error in ContainerManager");
- } catch (javax.resource.spi.SecurityException e) {
+ } catch (SecurityException e) {
throw convertToSQLException(e, "Authentication error. Invalid credentials");
} catch (ResourceAdapterInternalException e) {
throw convertToSQLException(e, "JDBC Connection problem");
@@ -114,7 +122,7 @@
return logTimeout;
}
- public java.io.PrintWriter getLogWriter() {
+ public PrintWriter getLogWriter() {
return logWriter;
}
@@ -123,7 +131,7 @@
logTimeout = seconds;
}
- public void setLogWriter(java.io.PrintWriter out) {
+ public void setLogWriter(PrintWriter out) {
logWriter = out;
}
}
Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionRequestInfo.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionRequestInfo.java?view=diff&rev=518446&r1=518445&r2=518446
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionRequestInfo.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/JdbcConnectionRequestInfo.java Wed Mar 14 19:38:05 2007
@@ -19,12 +19,10 @@
import javax.resource.spi.ConnectionRequestInfo;
public class JdbcConnectionRequestInfo implements ConnectionRequestInfo {
-
private String userName;
private String password;
private String jdbcDriver;
private String jdbcUrl;
- private int hashCode;
public JdbcConnectionRequestInfo(String userName, String password, String jdbcDriver, String jdbcUrl) {
this.userName = userName;
@@ -49,21 +47,24 @@
return jdbcUrl;
}
- public boolean equals(java.lang.Object other) {
- if (other instanceof JdbcConnectionRequestInfo &&
- ((JdbcConnectionRequestInfo) other).getUserName().equals(userName) &&
- ((JdbcConnectionRequestInfo) other).getPassword().equals(password) &&
- ((JdbcConnectionRequestInfo) other).getJdbcDriver().equals(jdbcDriver) &&
- ((JdbcConnectionRequestInfo) other).getJdbcUrl().equals(jdbcUrl))
- return true;
- else
- return false;
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ JdbcConnectionRequestInfo that = (JdbcConnectionRequestInfo) o;
+
+ return jdbcDriver.equals(that.jdbcDriver) &&
+ jdbcUrl.equals(that.jdbcUrl) &&
+ password.equals(that.password) &&
+ userName.equals(that.userName);
}
public int hashCode() {
- if (hashCode != 0) return hashCode;
- hashCode = jdbcDriver.hashCode() ^ jdbcUrl.hashCode() ^ userName.hashCode() ^ password.hashCode();
- return hashCode;
+ int result;
+ result = userName.hashCode();
+ result = 31 * result + password.hashCode();
+ result = 31 * result + jdbcDriver.hashCode();
+ result = 31 * result + jdbcUrl.hashCode();
+ return result;
}
-
}
Added: incubator/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/JdbcConfigTest.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/JdbcConfigTest.java?view=auto&rev=518446
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/JdbcConfigTest.java (added)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/JdbcConfigTest.java Wed Mar 14 19:38:05 2007
@@ -0,0 +1,123 @@
+/**
+ *
+ * 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.openejb.assembler.classic;
+
+import junit.framework.TestCase;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.config.sys.Connector;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JdbcConfigTest extends TestCase {
+ public void test() throws Exception {
+ ConfigurationFactory config = new ConfigurationFactory();
+ Assembler assembler = new Assembler();
+
+ // System services
+ assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
+ assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
+ assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
+
+ // connection manager
+ assembler.createConnectionManager(config.configureService(ConnectionManagerInfo.class));
+
+ // managed JDBC
+ assembler.createConnector(config.configureService(ConnectorInfo.class));
+
+ // unmanaged JDBC
+ Connector connector = new Connector();
+ connector.setId("Default Unmanaged JDBC Database");
+ connector.setProvider("Default Unmanaged JDBC Database");
+ assembler.createConnector(config.configureService(connector, ConnectorInfo.class));
+
+ ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
+
+ DataSource managedDS = (DataSource) containerSystem.getJNDIContext().lookup("java:openejb/Connector/Default JDBC Database");
+ assertNotNull("managedDS is null", managedDS);
+
+ DataSource unmanagedDS = (DataSource) containerSystem.getJNDIContext().lookup("java:openejb/Connector/Default Unmanaged JDBC Database");
+ assertNotNull("unmanagedDS is null", unmanagedDS);
+
+ List<Connection> managedConnections = new ArrayList<Connection>();
+ try {
+ for (int i = 0; i < 4; i++) {
+ Connection connection = managedDS.getConnection();
+ managedConnections.add(connection);
+
+ try {
+ connection.setAutoCommit(true);
+ fail("expected connection.setAutoCommit(true) to throw an exception");
+ } catch (SQLException expected) {
+ }
+
+ Statement statement = connection.createStatement();
+ try {
+ statement.getQueryTimeout();
+ } finally {
+ statement.close();
+ }
+ }
+ } finally {
+ for (Connection connection : managedConnections) {
+ close(connection);
+ }
+ }
+
+ List<Connection> unmanagedConnections = new ArrayList<Connection>();
+ try {
+ for (int i = 0; i < 4; i++) {
+ Connection connection = unmanagedDS.getConnection();
+ unmanagedConnections.add(connection);
+ assertTrue("Expected connection.getAutoCommit() to be true", connection.getAutoCommit());
+ connection.setAutoCommit(true);
+ Statement statement = connection.createStatement();
+ try {
+ statement.getQueryTimeout();
+ } finally {
+ statement.close();
+ }
+ connection.commit();
+ connection.setAutoCommit(false);
+ }
+ } finally {
+ for (Connection connection : unmanagedConnections) {
+ close(connection);
+ }
+ }
+
+ }
+
+ private static void close(Connection connection) {
+ if (connection == null) return;
+
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ }
+ }
+}