You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jason Perrone <ja...@blueskytech.com> on 2011/12/02 21:23:09 UTC

RE: Casting a Connection as an OracleConnection in Tomcat 7

So, I can't cast the connection Tomcat returns as a DelegatingConnection.  DelegatingConnection is from Commons DBCP and this connection is org.apache.tomcat.dbcp.dbcp.PoolableConnection.  I do not want to reference any Tomcat libraries directly because our product is container agnostic.  Any suggestions would be greatly appreciated.

Thank you.


-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net] 
Sent: Tuesday, November 29, 2011 5:08 PM
To: Tomcat Users List
Subject: Re: Casting a Connection as an OracleConnection in Tomcat 7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 11/29/11 2:34 PM, Jason Perrone wrote:
> Has anyone yet figured out how to cast the PoolableConnection that 
> Tomcat 7 now returns when you get a Connection from a DataSource as an 
> OracleConnection?

If you are using the standard pool (which uses DBCP under the hood), I think you get use getInnermostDelegate after casting to DelegatingConnection. That thing that gets returned should be an OracleConnection.

You also might need to set accessToUnderlyingConnectionAllowed on the DataSource in order to actually call getInnermostDelegate. See the DBCP configuration page and look for that setting for more info.

As always, remember to close the connection you got from the DataSource, not the inner delegate, or you will be toast.

> In Tomcat 6 the connection was returned as T4CConnection and had no 
> problem being cast as an OracleConnection. Now, I just can't figure it 
> out. Tried using the Commons DBCP package to get native JDBC 
> connections, and every other thing I found by Googling it.

What did you actually try? What is the runtime type of the Connection object returned from DataSource.getConnection? For me, it's BasicDataSource.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7VV8wACgkQ9CaO5/Lv0PDTOgCgqC3q35DFDoGaCGsdWwnRfN48
k5gAn3J+wzFTrzN3kmrsLWDAnLxHNxBo
=ro6R
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Casting a Connection as an OracleConnection in Tomcat 7

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 12/2/11 3:54 PM, Jason Perrone wrote:
> Yep, screw casting as an Oracle connection.

That would be my recommendation. People used to cast to
OracleConnection because JDBC didn't have support for LOB types way
back in the day. Some old online HOWTO must have been the meeting
place for Google searchers to "discover" that great hint to make your
life into a living hell by using underlying connections when it's not
necessary.

> Just do this:
> 
> session.beginTransaction(); PreparedStatement st =
> session.connection().prepareStatement("alter session set
> TIME_ZONE='" + timeZone.getID() + "'");

If you're going to use a PreparedStatement, you may as well actually
used the parametric replacement that it offers. :)

> Thanks Christopher, for getting me to think outside the box :)

I just think inside a different box.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7ZQO0ACgkQ9CaO5/Lv0PDDgQCdEkkWqfO7QC/zGxeV644EjE3U
iGoAn3XtoIX93+3+uoCs97mP08MhBtQ3
=L1fD
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Casting a Connection as an OracleConnection in Tomcat 7

Posted by Jason Perrone <ja...@blueskytech.com>.
Bingo.

Yep, screw casting as an Oracle connection.  Just do this:

session.beginTransaction();
PreparedStatement st = session.connection().prepareStatement("alter session set TIME_ZONE='" + timeZone.getID() + "'");
st.execute();
session.connection().commit();
st.close();

Thanks Christopher, for getting me to think outside the box :)



-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net] 
Sent: Friday, December 02, 2011 3:29 PM
To: Tomcat Users List
Subject: Re: Casting a Connection as an OracleConnection in Tomcat 7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 12/2/11 3:23 PM, Jason Perrone wrote:
> So, I can't cast the connection Tomcat returns as a 
> DelegatingConnection.  DelegatingConnection is from Commons DBCP and 
> this connection is org.apache.tomcat.dbcp.dbcp.PoolableConnection.  I 
> do not want to reference any Tomcat libraries directly because our 
> product is container agnostic.  Any suggestions would be greatly 
> appreciated.

Two thoughts:

1. Use reflection ;)

2. Stop trying to cast to OracleConnection

What do you need OracleConnection for, anyway?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7ZNSgACgkQ9CaO5/Lv0PA8iACeIyzpkgEx7B49HKcx7ZquASYK
UXEAnRAL4dcEPFENnatgp0WSJ+rQXMAN
=AWRl
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Casting a Connection as an OracleConnection in Tomcat 7

Posted by Jason Perrone <ja...@blueskytech.com>.
I have a customer who is using Oracle Timestamps with TIMEZONE and in order to get any dates out of this database you have to call an Oracle specific JDBC method to set the TimeZone on the session.  So, you have to cast the connection to an OracleConnection so you have access to that set timezone on session method.  It's stupid, because our application handles timezones, but they've got their policies, ya know. LOL.

Care to elaborate on the "use reflection" suggestion? :)



-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net] 
Sent: Friday, December 02, 2011 3:29 PM
To: Tomcat Users List
Subject: Re: Casting a Connection as an OracleConnection in Tomcat 7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 12/2/11 3:23 PM, Jason Perrone wrote:
> So, I can't cast the connection Tomcat returns as a 
> DelegatingConnection.  DelegatingConnection is from Commons DBCP and 
> this connection is org.apache.tomcat.dbcp.dbcp.PoolableConnection.  I 
> do not want to reference any Tomcat libraries directly because our 
> product is container agnostic.  Any suggestions would be greatly 
> appreciated.

Two thoughts:

1. Use reflection ;)

2. Stop trying to cast to OracleConnection

What do you need OracleConnection for, anyway?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7ZNSgACgkQ9CaO5/Lv0PA8iACeIyzpkgEx7B49HKcx7ZquASYK
UXEAnRAL4dcEPFENnatgp0WSJ+rQXMAN
=AWRl
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Casting a Connection as an OracleConnection in Tomcat 7

Posted by Martin Gainty <mg...@hotmail.com>.
Here is why:
>javap oracle.jdbc.OracleConnection | grep oracle
public interface oracle.jdbc.OracleConnection extends java.sql.Connection{
    public abstract void registerTAFCallback(oracle.jdbc.OracleOCIFailover, java
.lang.Object)       throws java.sql.SQLException;
    public abstract oracle.jdbc.OracleConnection unwrap();
    public abstract void setWrapper(oracle.jdbc.OracleConnection);
    public abstract oracle.jdbc.internal.OracleConnection physicalConnectionWith
in();
    public abstract oracle.jdbc.OracleSavepoint oracleSetSavepoint()       throw
s java.sql.SQLException;
    public abstract oracle.jdbc.OracleSavepoint oracleSetSavepoint(java.lang.Str
ing)       throws java.sql.SQLException;
    public abstract void oracleRollback(oracle.jdbc.OracleSavepoint)       throw
s java.sql.SQLException;
    public abstract void oracleReleaseSavepoint(oracle.jdbc.OracleSavepoint)
   throws java.sql.SQLException;
    public abstract void registerConnectionCacheCallback(oracle.jdbc.pool.Oracle
ConnectionCacheCallback, java.lang.Object, int)       throws java.sql.SQLException; i agree..stick with OracleConnection for Oracle...all else use java.sql.Connection
Martin 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.

 > From: jason.perrone@blueskytech.com
> To: users@tomcat.apache.org
> Date: Fri, 2 Dec 2011 14:39:11 -0600
> Subject: RE: Casting a Connection as an OracleConnection in Tomcat 7
> 
> I might be able to do it as an SQL statement instead of using this driver method... 
> 
> 
> -----Original Message-----
> From: Christopher Schultz [mailto:chris@christopherschultz.net] 
> Sent: Friday, December 02, 2011 3:29 PM
> To: Tomcat Users List
> Subject: Re: Casting a Connection as an OracleConnection in Tomcat 7
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Jason,
> 
> On 12/2/11 3:23 PM, Jason Perrone wrote:
> > So, I can't cast the connection Tomcat returns as a 
> > DelegatingConnection.  DelegatingConnection is from Commons DBCP and 
> > this connection is org.apache.tomcat.dbcp.dbcp.PoolableConnection.  I 
> > do not want to reference any Tomcat libraries directly because our 
> > product is container agnostic.  Any suggestions would be greatly 
> > appreciated.
> 
> Two thoughts:
> 
> 1. Use reflection ;)
> 
> 2. Stop trying to cast to OracleConnection
> 
> What do you need OracleConnection for, anyway?
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAk7ZNSgACgkQ9CaO5/Lv0PA8iACeIyzpkgEx7B49HKcx7ZquASYK
> UXEAnRAL4dcEPFENnatgp0WSJ+rQXMAN
> =AWRl
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
 		 	   		  

RE: Casting a Connection as an OracleConnection in Tomcat 7

Posted by Jason Perrone <ja...@blueskytech.com>.
I might be able to do it as an SQL statement instead of using this driver method... 


-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net] 
Sent: Friday, December 02, 2011 3:29 PM
To: Tomcat Users List
Subject: Re: Casting a Connection as an OracleConnection in Tomcat 7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 12/2/11 3:23 PM, Jason Perrone wrote:
> So, I can't cast the connection Tomcat returns as a 
> DelegatingConnection.  DelegatingConnection is from Commons DBCP and 
> this connection is org.apache.tomcat.dbcp.dbcp.PoolableConnection.  I 
> do not want to reference any Tomcat libraries directly because our 
> product is container agnostic.  Any suggestions would be greatly 
> appreciated.

Two thoughts:

1. Use reflection ;)

2. Stop trying to cast to OracleConnection

What do you need OracleConnection for, anyway?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7ZNSgACgkQ9CaO5/Lv0PA8iACeIyzpkgEx7B49HKcx7ZquASYK
UXEAnRAL4dcEPFENnatgp0WSJ+rQXMAN
=AWRl
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Casting a Connection as an OracleConnection in Tomcat 7

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 12/2/11 3:23 PM, Jason Perrone wrote:
> So, I can't cast the connection Tomcat returns as a 
> DelegatingConnection.  DelegatingConnection is from Commons DBCP
> and this connection is
> org.apache.tomcat.dbcp.dbcp.PoolableConnection.  I do not want to
> reference any Tomcat libraries directly because our product is
> container agnostic.  Any suggestions would be greatly appreciated.

Two thoughts:

1. Use reflection ;)

2. Stop trying to cast to OracleConnection

What do you need OracleConnection for, anyway?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7ZNSgACgkQ9CaO5/Lv0PA8iACeIyzpkgEx7B49HKcx7ZquASYK
UXEAnRAL4dcEPFENnatgp0WSJ+rQXMAN
=AWRl
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org