mirror of https://github.com/apache/openjpa.git
OPENJPA-1354: DBCP getConnection with user/password
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@830426 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ad1029fde4
commit
3ec0fbf66f
|
@ -20,6 +20,7 @@ package org.apache.openjpa.lib.jdbc;
|
|||
|
||||
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 @@ public abstract class DelegatingDataSource implements DataSource, Closeable {
|
|||
throws SQLException {
|
||||
if (user == null && pass == null)
|
||||
return _ds.getConnection();
|
||||
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 @@ public abstract class DelegatingDataSource implements DataSource, Closeable {
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue