|
Hello,
this is dbcp related.
I need to perform the following operation just
after having created a connection :
execute statement "alter session set
nls_date_format= 'DD/MM/YYYY'" (oracle specific)
after reading the dbcp source code, I guess that
such an init can be done around DriverConnectionFactory.createConnection(),
either just after in a calling class or by subclassing DriverConnectionFactory.
I wrote a subclass called DbiOracleDriverConnectionFactory to that effect (see
attachment)
It looks like in either case I need to rewrite
BasicDataSource and BasicDataSourceFactory. A lot of work for such a common
(sic) operation as is "feeding some command at initialization time".
for the moment, I wrote a patch to BasicDataSource
and BasicDataSourceFactory where the class name of the DriverConnectionFactory
can be set in server.xml, passed from BasicDataSourceFactory to
BasicDataSource, and be used by the later in its createDataSource() method
(see attachment)
isn't there something more simple to do ? is there
a better way to do it ? am I the only one to need to pass some commands at
connection init time ? do I miss something ?
Christophe Thi�baud
|
diff -r1.3 BasicDataSourceFactory.java
186c186
< if (ra != null) {
---
> if (ra != null) {
194a195,200
> }
>
> ra = ref.get("driverConnectionFactoryClassName");
> if (ra != null) {
> dataSource.setDriverConnectionFactoryClassName
> (ra.getContent().toString());
diff -r1.9 BasicDataSource.java
74a75
> import java.lang.reflect.Constructor;
269a271,282
> /**
> * The fully qualified Java class name of the DriverConnectionFactory to be user
> */
> protected String driverConnectionFactoryClassName =
>"org.apache.commons.dbcp.DriverConnectionFactory";
>
> public String getDriverConnectionFactoryClassName() {
> return (this.driverConnectionFactoryClassName);
> }
>
> public void setDriverConnectionFactoryClassName(String
>driverConnectionFactoryClassName) {
> this.driverConnectionFactoryClassName = driverConnectionFactoryClassName;
> }
388c401
< /**
---
> /**
395,398c408,411
< * Setting this to true can recover db connections from poorly written
< * applications which fail to close a connection.
< */
< public boolean getRemoveAbandoned() {
---
> * Setting this to true can recover db connections from poorly written
> * applications which fail to close a connection.
> */
> public boolean getRemoveAbandoned() {
403,404c416,417
< }
<
---
> }
>
410,411c423,424
< }
<
---
> }
>
415,417c428,430
< * Defaults to 300 seconds.
< */
< public int getRemoveAbandonedTimeout() {
---
> * Defaults to 300 seconds.
> */
> public int getRemoveAbandonedTimeout() {
422,423c435,436
< }
<
---
> }
>
429,430c442,443
< }
<
---
> }
>
435,436c448,449
< * Defaults to false.
< *
---
> * Defaults to false.
> *
438,441c451,454
< * for every Connection open or new Statement because a stack
< * trace has to be generated.
< */
< public boolean getLogAbandoned() {
---
> * for every Connection open or new Statement because a stack
> * trace has to be generated.
> */
> public boolean getLogAbandoned() {
446,447c459,460
< }
<
---
> }
>
563,564c576,591
< DriverConnectionFactory driverConnectionFactory =
< new DriverConnectionFactory(driver, url, connectionProperties);
---
> DriverConnectionFactory driverConnectionFactory = null;
> try {
> Class[] argsClass = new Class[] {Driver.class, String.class,
>Properties.class};
> Object[] args = new Object[] {driver, url,
>connectionProperties};
> Class clazz =
>Class.forName(driverConnectionFactoryClassName);
> Constructor constructor = clazz.getConstructor(argsClass);
> Object o = constructor.newInstance(args);
> driverConnectionFactory = (DriverConnectionFactory)o;
>
> } catch (Throwable t) {
> String message = "Cannot load DBCP connectionFactory '" +
> driverConnectionFactoryClassName + "'";
> logWriter.println(message);
> t.printStackTrace(logWriter);
> throw new SQLException(message);
> }
package it.unimaticaspa.unique.database;
import org.apache.commons.dbcp.DriverConnectionFactory;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class DbiOracleDriverConnectionFactory extends DriverConnectionFactory {
public DbiOracleDriverConnectionFactory(Driver driver, String connectUri, Properties props) {
super(driver, connectUri, props);
}
public Connection createConnection() {
Connection conn = super.createConnection();
try {
DbiOracleDataSource.setDateFormat(conn);
} catch(SQLException e) {
throw new RuntimeException(e.toString());
}
return conn;
}
}
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
