From: Jonatan Kronqvist Date: Thu, 22 Sep 2011 08:49:46 +0000 (+0000) Subject: Fixed npe in SQLContainer's J2EEConnectionPool #7597 X-Git-Tag: 6.7.0.rc1~35 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=02dbee6888c87286e41784adf80d95d676178841;p=vaadin-framework.git Fixed npe in SQLContainer's J2EEConnectionPool #7597 svn changeset:21225/svn branch:6.7 --- diff --git a/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java b/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java index 09c9cc1b1b..12a2c6cd9f 100644 --- a/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java +++ b/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java @@ -5,12 +5,16 @@ package com.vaadin.data.util.sqlcontainer.connection; import java.sql.Connection; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class J2EEConnectionPool implements JDBCConnectionPool { + private static final Logger logger = Logger + .getLogger(J2EEConnectionPool.class.getName()); private String dataSourceJndiName; @@ -50,10 +54,12 @@ public class J2EEConnectionPool implements JDBCConnectionPool { } public void releaseConnection(Connection conn) { - try { - conn.close(); - } catch (SQLException e) { - e.printStackTrace(); + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + logger.log(Level.FINE, "Could not release SQL connection", e); + } } } diff --git a/tests/src/com/vaadin/tests/server/container/sqlcontainer/connection/J2EEConnectionPoolTest.java b/tests/src/com/vaadin/tests/server/container/sqlcontainer/connection/J2EEConnectionPoolTest.java index dc5ca51d94..fd0c5b9af7 100644 --- a/tests/src/com/vaadin/tests/server/container/sqlcontainer/connection/J2EEConnectionPoolTest.java +++ b/tests/src/com/vaadin/tests/server/container/sqlcontainer/connection/J2EEConnectionPoolTest.java @@ -65,7 +65,8 @@ public class J2EEConnectionPoolTest { ds.getConnection(); EasyMock.expectLastCall().andReturn(connection); - System.setProperty("java.naming.factory.initial", + System.setProperty( + "java.naming.factory.initial", "com.vaadin.tests.server.container.sqlcontainer.connection.MockInitialContextFactory"); Context context = EasyMock.createMock(Context.class); context.lookup("testDataSource"); @@ -94,8 +95,17 @@ public class J2EEConnectionPoolTest { EasyMock.replay(context); J2EEConnectionPool pool = new J2EEConnectionPool("foo"); - Connection c = pool.reserveConnection(); + pool.reserveConnection(); EasyMock.verify(context); } + @Test + public void releaseConnection_null_shouldSucceed() throws SQLException { + DataSource ds = EasyMock.createMock(DataSource.class); + EasyMock.replay(ds); + + J2EEConnectionPool pool = new J2EEConnectionPool(ds); + pool.releaseConnection(null); + EasyMock.verify(ds); + } }