Author: johnthuss
Date: Tue Aug 14 20:47:05 2012
New Revision: 1373077
URL: http://svn.apache.org/viewvc?rev=1373077&view=rev
Log:
Lazily instantiate jdbc driver class to prevent errors for connections that are
never opened
Added:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conn/DriverDataSourceTest.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java?rev=1373077&r1=1373076&r2=1373077&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conn/DriverDataSource.java
Tue Aug 14 20:47:05 2012
@@ -39,8 +39,9 @@ import org.apache.cayenne.util.Util;
*/
public class DriverDataSource implements DataSource {
- protected Driver driver;
-
+ protected Driver _driver;
+ protected String driverClassName;
+
protected String connectionUrl;
protected String userName;
protected String password;
@@ -82,8 +83,7 @@ public class DriverDataSource implements
* null, a user must take care of registering the driver. "connectionUrl"
on the other
* hand must NOT be null.
*/
- public DriverDataSource(String driverClassName, String connectionUrl)
- throws SQLException {
+ public DriverDataSource(String driverClassName, String connectionUrl) {
this(driverClassName, connectionUrl, null, null);
}
@@ -96,7 +96,7 @@ public class DriverDataSource implements
* @since 3.0
*/
public DriverDataSource(String driverClassName, String connectionUrl,
- String userName, String password) throws SQLException {
+ String userName, String password) {
setDriverClassName(driverClassName);
@@ -116,7 +116,8 @@ public class DriverDataSource implements
public DriverDataSource(Driver driver, String connectionUrl, String
userName,
String password) {
- this.driver = driver;
+ this._driver = driver;
+ this.driverClassName = driver.getClass().getName();
this.connectionUrl = connectionUrl;
this.userName = userName;
this.password = password;
@@ -143,7 +144,7 @@ public class DriverDataSource implements
Connection c = null;
- if (driver == null) {
+ if (getDriver() == null) {
c = DriverManager.getConnection(connectionUrl, userName,
password);
}
else {
@@ -156,7 +157,7 @@ public class DriverDataSource implements
if (password != null) {
connectProperties.put("password", password);
}
- c = driver.connect(connectionUrl, connectProperties);
+ c = getDriver().connect(connectionUrl, connectProperties);
}
// some drivers (Oracle) return null connections instead of
throwing
@@ -248,13 +249,24 @@ public class DriverDataSource implements
}
public String getDriverClassName() {
- return driver != null ? driver.getClass().getName() : null;
+ return driverClassName;
}
- public void setDriverClassName(String driverClassName) throws SQLException
{
+ public void setDriverClassName(String driverClassName) {
if (!Util.nullSafeEquals(getDriverClassName(), driverClassName)) {
- this.driver = driverClassName != null ?
loadDriver(driverClassName) : null;
+ this.driverClassName = driverClassName;
+ this._driver = null; // force reload
+ }
+ }
+
+ /**
+ * Lazily instantiate the driver class to prevent errors for connections
that are never opened
+ */
+ private Driver getDriver() throws SQLException {
+ if (_driver == null && driverClassName != null) {
+ _driver = loadDriver(driverClassName);
}
+ return _driver;
}
/**
Added:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conn/DriverDataSourceTest.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conn/DriverDataSourceTest.java?rev=1373077&view=auto
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conn/DriverDataSourceTest.java
(added)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conn/DriverDataSourceTest.java
Tue Aug 14 20:47:05 2012
@@ -0,0 +1,39 @@
+/*****************************************************************
+ * 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.cayenne.conn;
+
+import java.sql.SQLException;
+
+import junit.framework.TestCase;
+
+public class DriverDataSourceTest extends TestCase {
+
+ public void testLazyInstantiationOfDriverClass() {
+ DriverDataSource dataSource = new
DriverDataSource("does.not.exist.Driver",
"jdbc:postgresql://localhost/database");
+ assertNotNull(dataSource);
+
+ try {
+ dataSource.getConnection();
+ fail();
+ } catch (SQLException e) {
+ // expected because driver class does not exist
+ }
+ }
+
+}