Thank you, David. Unfortunately, I still get the exception:
Caused by: java.lang.ClassCastException:
org.apache.commons.dbcp.DelegatingCallableStatement cannot be cast to
oracle.jdbc.OracleCallableStatement
at com.bmi.sms.ejb.company.CompanyDao.getCompanyInfo(CompanyDao.java:48)
line 47: DelegatingCallableStatement dbcpStatement =
(DelegatingCallableStatement)stmt;
line 48: OracleCallableStatement oracleStatement =
(OracleCallableStatement) dbcpStatement.getDelegate();
It looks like getDelegate() returns a DelegatingCallableStatement.
I found another work around, though. I wrote a class that implements
ObjectFactory, jarred it, and placed it in the Tomcat/lib directory.
public class MyOracleDataSourceFactory implements ObjectFactory {
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment) throws Exception {
// Acquire an instance of our specified bean class
OracleDataSource ds = new OracleDataSource();
// Customize the bean properties from our attributes
Reference ref = (Reference) obj;
Enumeration<RefAddr> addrs = ref.getAll();
while (addrs.hasMoreElements()) {
RefAddr addr = addrs.nextElement();
String aname = addr.getType();
String value = (String) addr.getContent();
if (aname.equals("url")) {
ds.setURL(value);
} else if (aname.equals("username")) {
ds.setUser(value);
} else if (aname.equals("password")) {
ds.setPassword(value);
}
}
// Return the customized instance
return (ds);
}
}
Then I added this <Resource> element to tomcat/conf/server.xml
<Resource name="jdbc/JUSERDS"
auth="Container"
type="javax.sql.DataSource"
factory="MyOracleDataSourceFactory"
url="jdbc:oracle:thin:@naxtest01:1523:SMST"
username="xxxx"
password="yyyy"
/>
This is documented in tomcat/docs. It works great and keeps dbcp package
out of source code.
Thanks again for your help.
-Henry
David Blevins wrote:
>
> Not giving out direct references to the driver-created DataSources is
> an unavoidable part of connection pooling -- the concept is built on
> giving out several "handles" which delegate to a DataSource which is
> tracked in the transaction associated with the current thread and
> returned to the pool when the transaction completes.
>
> That said, you can still get a reference to the driver-created
> DataSource via a call to the connection pooling library we use
> (commons-dbcp).
>
> import java.sql.CallableStatement;
> import org.apache.commons.dbcp.DelegatingCallableStatement;
> import oracle.jdbc.OracleCallableStatement;
>
> ....
>
> public void someMethod() throws Exception {
> CallableStatement callableStatement = ... // get it as you
> normally would.
> DelegatingCallableStatement dbcpStatement =
> (DelegatingCallableStatement)callableStatement;
> OracleCallableStatement oracleStatement =
> (OracleCallableStatement) dbcpStatement.getDelegate();
> }
>
> The line to note is the DelegatingCallableStatement.getDelegate()
> method which will return the physical CallableStatement created by the
> OracleDriver. Note, you do not want to be holding on to this for very
> long -- definitely not beyond the scope of one transaction.
>
> Best to grab it, do the work you need, and throw it away by just not
> keeping a reference to it. Otherwise you'll likely encounter major
> connection and memory leeks if you hold onto that object beyond the
> scope of a transaction -- this is what the wrapping concept normally
> prevents.
>
> Hope this helps!
>
> -David
>
> On Jan 21, 2009, at 12:27 PM, hretter wrote:
>
>>
>> I get a ClassCastException:
>>
>> java.lang.ClassCastException:
>> org.apache.commons.dbcp.DelegatingCallableStatement cannot be cast to
>> oracle.jdbc.OracleCallableStatement
>>
>> because openejb is not loading the oracle driver as described in
>> openejb.xml:
>>
>> <Resource id="JUSERDS" type="DataSource">
>> JdbcDriver oracle.jdbc.OracleDriver
>> JdbcUrl jdbc:oracle:thin:@naxtest01:1523:SMST
>> UserName xxxx
>> Password yyyy
>> JtaManaged true
>> </Resource>
>>
>> java source ----------------------------------
>>
>> @Resource(name = "JUSERDS")
>> private DataSource _ds;
>> ...
>> ...
>> @PostConstruct
>> void init() {
>> _logger.debug("init DataSource=" + _ds);
>> _dao = new CompanyDao(_ds);
>> }
>>
>> log output on above line:
>> 2009-01-21 14:11:55,095 [http-8080-1] DEBUG
>> com.bmi.sms.ejb.company.Company
>> - init
>> datasource=org.apache.openejb.resource.jdbc.basicdatasou...@82f392
>>
>> I'm fetching an Oracle Cursor, so I need to cast the Statement.
>> Thanks.
>>
>> -Henry Retter
>> --
>> View this message in context:
>> http://www.nabble.com/DataSource-problem-tp21591543p21591543.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>>
>
>
>
--
View this message in context:
http://www.nabble.com/DataSource-problem-tp21591543p21593376.html
Sent from the OpenEJB User mailing list archive at Nabble.com.