Native JDBC driver access

349 views
Skip to first unread message

Jakub Kotowski

unread,
Sep 24, 2011, 5:57:28 AM9/24/11
to play-framework
Hi,

I'm using PostgreSQL and I need to use some of the PostgreSQL-specific
JDBC extension (such as geometric types). So far I've learned that the
normal simple approach (getting a connection and casting it to
PGConnection) is not possible because with JPA2 it is impossible to
get to the original implementation - I always get only Proxys and a
ClassCastException. It's probably the same with using the C3P0
directly (I know there is some "reflective" way that the C3P0 provides
for accessing native features but I find it not very nice).

I guess what I could do is to write my own "PostgreSQLDBPlugin" that
would create a native PostgreSQL DataSource. If I do this, how should
I use it along with the Play's DBPlugin? Is there a way to switch the
plugin off? Or should my plugin run after the Play's plugin and just
set the DB.datasource again?

Is there some other way of using database-specific features while not
abandoning JPA?

Regards,

Jakub

Rakesh Waghela

unread,
Sep 24, 2011, 1:38:36 PM9/24/11
to play-fr...@googlegroups.com
Checkout http://jooq.org  which is specifically developed to gain advantage of database specific features while maintaining the good accessibility.

Jakub Kotowski

unread,
Sep 24, 2011, 5:42:09 PM9/24/11
to play-framework
Hi Rakesh,

thanks for the pointer, JOOQ looks certainly interesting but I don't
see how it helps to solve my current problem. I'll keep it in mind in
case I rewrite my application one day.

Regards,

Jakub


On Sep 24, 7:38 pm, Rakesh Waghela <javain...@gmail.com> wrote:
> Checkouthttp://jooq.org which is specifically developed to gain advantage

Jonas

unread,
Sep 25, 2011, 4:31:27 AM9/25/11
to play-framework
You can use JDBC directly:

Connection conn = play.db.DB.getConnection()

/Jonas

Jakub Kotowski

unread,
Sep 25, 2011, 5:27:27 AM9/25/11
to play-framework
no, this way you get only a proxy wrapped around the original driver
implementation. Try:

Connection conn = play.db.DB.getConnection()
PGConnection pgconn = (PGConnection) conn; //or whatever JDBC driver
you use

and you'll get a ClassCastException.

Adam Hooper

unread,
Oct 24, 2012, 6:30:58 PM10/24/12
to play-fr...@googlegroups.com
How about "PGConnection pgconn = conn.unwrap(classOf[PGConnection])"?

Enjoy life,
Adam

Adam Hooper

unread,
Oct 24, 2012, 6:31:57 PM10/24/12
to play-fr...@googlegroups.com
How about I write that properly:

Java: PGConnection pgconn = conn.unwrap(PGConnection.class)

Scala: pgconn : PGConnection = conn.unwrap(classOf[PGConnection])

Evidently, I haven't tested either :).

Enjoy life,
Adam

Jonas K

unread,
Oct 25, 2012, 1:45:32 PM10/25/12
to play-fr...@googlegroups.com


On Wednesday, October 24, 2012 6:31:58 PM UTC-4, Adam Hooper wrote:
How about I write that properly:

Java: PGConnection pgconn = conn.unwrap(PGConnection.class)

Scala: pgconn : PGConnection = conn.unwrap(classOf[PGConnection])

For me the above results in 

PSQLException: Method org.postgresql.jdbc4.Jdbc4Connection.unwrap(Class<T>) is not yet implemented. (AbstractJdbc4Connection.java:163)
 

conn match {
  case bcpConn: com.jolbox.bonecp.ConnectionHandle => {
     bcpConn.getInternalConnection() match {
       case pgConn: org.postgresql.PGConnection => {
          val loMgr = pgConn.getLargeObjectAPI()
           ...
       }
       case _ => // handle bad cast
    }
   case _ => // handle bad cast
 }

Seems to work

_jonas
 
 

ennala

unread,
Nov 12, 2014, 12:23:10 AM11/12/14
to play-fr...@googlegroups.com
Hi Jonas,

can you write that code in java.
I am facing the same situation but I was not able to understand you code.

Thanks.
Reply all
Reply to author
Forward
0 new messages