From 02dbee6888c87286e41784adf80d95d676178841 Mon Sep 17 00:00:00 2001 From: Jonatan Kronqvist Date: Thu, 22 Sep 2011 08:49:46 +0000 Subject: [PATCH] Fixed npe in SQLContainer's J2EEConnectionPool #7597 svn changeset:21225/svn branch:6.7 --- .../connection/J2EEConnectionPool.java | 14 ++++++++++---- .../connection/J2EEConnectionPoolTest.java | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) 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); + } } -- 2.39.5