aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-06-10 15:24:57 +0200
committersimonbrandhof <simon.brandhof@gmail.com>2011-06-10 15:24:57 +0200
commit78e9b96a51e2ef677a4eb8b801e18248d7e32283 (patch)
treee0bae2cb0f36df7ac10edddcb929683437abc2c1 /sonar-core/src
parent3203b81516eb93052a05b74c6e0a5ddc7f1977ca (diff)
parent231d8aeceb89848a8099f1d516f85d76106f3fef (diff)
downloadsonarqube-78e9b96a51e2ef677a4eb8b801e18248d7e32283.tar.gz
sonarqube-78e9b96a51e2ef677a4eb8b801e18248d7e32283.zip
Merge remote branch 'upstream/master'
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/session/DriverDatabaseConnector.java70
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java5
2 files changed, 50 insertions, 25 deletions
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/DriverDatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/DriverDatabaseConnector.java
index d30892f14b4..00e91fd8f38 100644
--- a/sonar-core/src/main/java/org/sonar/jpa/session/DriverDatabaseConnector.java
+++ b/sonar-core/src/main/java/org/sonar/jpa/session/DriverDatabaseConnector.java
@@ -27,11 +27,13 @@ import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.Enumeration;
import java.util.Properties;
public class DriverDatabaseConnector extends AbstractDatabaseConnector {
private ClassLoader classloader;
+ private boolean driverProxyRegistered = false;
public DriverDatabaseConnector(Configuration configuration) {
super(configuration, true);
@@ -74,25 +76,53 @@ public class DriverDatabaseConnector extends AbstractDatabaseConnector {
}
public Connection getConnection() throws SQLException {
- try {
- /*
- The sonar batch downloads the JDBC driver in a separated classloader.
- This is a well-know problem of java.sql.DriverManager. The workaround
- is to use a proxy.
- See http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-from-an-arbitrary-location
- */
- Driver driver = (Driver)classloader.loadClass(getDriver()).newInstance();
- DriverManager.registerDriver(new DriverProxy(driver));
-
- } catch (Exception e) {
- SQLException ex = new SQLException("SQL driver not found " + getDriver());
- ex.initCause(e);
- throw ex;
+ /*
+ * The Sonar batch downloads the JDBC driver in a separated class loader.
+ * This is a well-know problem of java.sql.DriverManager. The workaround
+ * is to use a proxy.
+ * See http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-from-an-arbitrary-location
+ */
+ if (!driverProxyRegistered) {
+ driverProxyRegistered = true;
+ try {
+ Driver driver = (Driver) classloader.loadClass(getDriver()).newInstance();
+ DriverManager.registerDriver(new DriverProxy(driver));
+ } catch (Exception e) {
+ SQLException ex = new SQLException("SQL driver not found " + getDriver());
+ throw (SQLException) ex.initCause(e);
+ }
}
return DriverManager.getConnection(getUrl(), getUsername(), getPassword());
}
@Override
+ public void stop() {
+ super.stop();
+
+ deregisterDriverProxy();
+ }
+
+ /**
+ * Due to memory leak in DriverManager we also should deregister original driver,
+ * but we can't do it here, because DriverManager checks the class loader of the calling class.
+ * So actually we might have a memory leak, but it supposed to be handled by Sonar batch.
+ */
+ private void deregisterDriverProxy() {
+ Enumeration<Driver> drivers = DriverManager.getDrivers();
+ while (drivers.hasMoreElements()) {
+ Driver driver = drivers.nextElement();
+ if (driver instanceof DriverProxy) {
+ try {
+ DriverManager.deregisterDriver(driver);
+ LOG.debug("JDBC Driver [{}] deregistered", driver);
+ } catch (SQLException e) {
+ LOG.warn("JDBC driver deregistration failed", e);
+ }
+ }
+ }
+ }
+
+ @Override
public void setupEntityManagerFactory(Properties factoryProps) {
factoryProps.put("hibernate.connection.url", getUrl());
factoryProps.put("hibernate.connection.driver_class", getDriver());
@@ -113,7 +143,7 @@ final class DriverProxy implements Driver {
DriverProxy(Driver target) {
if (target == null) {
- throw new NullPointerException();
+ throw new IllegalArgumentException();
}
this.target = target;
}
@@ -126,9 +156,7 @@ final class DriverProxy implements Driver {
return target.acceptsURL(url);
}
- public Connection connect(
- String url, Properties info
- ) throws SQLException {
+ public Connection connect(String url, Properties info) throws SQLException {
return target.connect(url, info);
}
@@ -140,9 +168,7 @@ final class DriverProxy implements Driver {
return target.getMinorVersion();
}
- public java.sql.DriverPropertyInfo[] getPropertyInfo(
- String url, Properties info
- ) throws SQLException {
+ public java.sql.DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return target.getPropertyInfo(url, info);
}
@@ -168,4 +194,4 @@ final class DriverProxy implements Driver {
org.sonar.jpa.session.DriverProxy other = (org.sonar.jpa.session.DriverProxy) obj;
return this.target.equals(other.target);
}
-} \ No newline at end of file
+}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java
index ab3ee2417d0..0ae8a5e516e 100644
--- a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java
+++ b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java
@@ -160,7 +160,7 @@ public class JpaDatabaseSession extends DatabaseSession {
startTransaction();
return entityManager.createQuery(hql);
}
-
+
@Override
public Query createNativeQuery(String sql) {
startTransaction();
@@ -214,8 +214,7 @@ public class JpaDatabaseSession extends DatabaseSession {
} catch (NonUniqueResultException ex) {
NonUniqueResultException e = new NonUniqueResultException("Expected single result for entitiy " + entityClass.getSimpleName()
+ " with criterias : " + StringUtils.join(criterias, ","));
- e.initCause(ex);
- throw e;
+ throw (NonUniqueResultException) e.initCause(ex);
}
}