views:

70

answers:

2

hi, is there any way, to restrict hibernate not to create connection of its own(what we define in hibernate.properties or hibernate.cfg.xml), instead i can create and pass the connection to hibernate for further processing.

The problem is that i need to set the ApplicationContext on the connection given that the i am using oracle connection. I know how to create a connection and set the applicationContext on to it.. but the problem is that i don't know how to force hibernate to use the connection that i have created.. Please help..

A: 

Have a look at this section of the Hibernate manual.

Bozhidar Batsov
i have tried with this approach but the problem is that i need to add application context to the connection, whereas according to the link you have referred , hibernate will again create connection of its own..as i am only programatically handling the hibernate.cfg or hibernate.proeprties file..
Mrityunjay
+1  A: 

The right way to do this would be to use a custom implementation of o.h.c.ConnectionProvider. In the getConnection() method, you will have the opportunity to cast the regular Connection into an OracleConnection and to do dark voodoo with it before to return it.

This interface has several implementations that you can extend to ease the work, depending on how you get the initial connection (e.g. from a Datasource).

This post in the Hibernate forums shows an implementation that could be used as a kickoff example (the poster is also doing black magic with an OracleConnection so it's a good example).

Pascal Thivent