1

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..

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
M.J.
  • 16,266
  • 28
  • 75
  • 97
  • Your use case is unclear. What is ApplicationContext? Spring? How do you set anything to a connection, since it is an interface that doesn't define ApplicationContext? – Bozho Jun 05 '10 at 05:56
  • hi Bozho, u r right that Connection interface does not have any method to set the application context, application context here in terms of connection is the user policy.. i have the piece of code that works fine to set the user policy... i can set the application context by getting the native oracle connection out of the connection.... and then on the native oracle connection i can set the application context... – M.J. Jun 05 '10 at 07:11

2 Answers2

2

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
  • 562,542
  • 136
  • 1,062
  • 1,124
0

Have a look at this section of the Hibernate manual.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • 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.. – M.J. Jun 05 '10 at 05:27