You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pp...@apache.org on 2009/10/28 04:00:17 UTC
svn commit: r830426 -
/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingDataSource.java
Author: ppoddar
Date: Wed Oct 28 03:00:17 2009
New Revision: 830426
URL: http://svn.apache.org/viewvc?rev=830426&view=rev
Log:
OPENJPA-1354: DBCP getConnection with user/password
Modified:
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingDataSource.java
Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingDataSource.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingDataSource.java?rev=830426&r1=830425&r2=830426&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingDataSource.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingDataSource.java Wed Oct 28 03:00:17 2009
@@ -20,6 +20,7 @@
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
@@ -134,7 +135,23 @@
throws SQLException {
if (user == null && pass == null)
return _ds.getConnection();
- return _ds.getConnection(user, pass);
+ try {
+ return _ds.getConnection(user, pass);
+ } catch (UnsupportedOperationException ex) {
+ // OPENJPA-1354
+ // under some configuration _ds is Commons DBCP Basic/Poolable DataSource
+ // that does not support getConnection(user, password)
+ // see http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html
+ // hence this workaround
+ try {
+ if (setBeanProperty(_ds, "setUsername", user)
+ && setBeanProperty(_ds, "setPassword", pass))
+ return _ds.getConnection();
+ } catch (Exception e) {
+ throw ex;
+ }
+ }
+ return null;
}
public void close() throws Exception {
@@ -153,4 +170,14 @@
else
return null;
}
+
+ private boolean setBeanProperty(Object target, String method, Object val) {
+ try {
+ Method setter = target.getClass().getMethod(method, new Class[]{});
+ setter.invoke(target, val);
+ return true;
+ } catch (Throwable t) {
+ return false;
+ }
+ }
}