如何将用户创建的连接传递给hibernate [英] how to pass user created connection to hibernate

查看:115
本文介绍了如何将用户创建的连接传递给hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法,限制hibernate不创建自己的连接(我们在hibernate.properties或hibernate.cfg.xml中定义),而是我可以创建和传递连接到hibernate进一步处理。 p>

问题是,我需要设置ApplicationContext的连接给定我使用oracle连接。我知道如何创建一个连接,并设置applicationContext到它..但问题是,我不知道如何强制hibernate使用我创建的连接..请帮助..

解决方案

正确的方法是使用 ohcConnectionProvider 的自定义实现。在 getConnection()方法中,您将有机会将常规 Connection 转换为



这个接口有几个实现,你可以扩展来简化工作,取决于你如何获得初始连接(例如从数据源)。



在Hibernate论坛中的这个帖子显示了一个可用作启动示例的实现(海报也使用 OracleConnection 所以这是一个很好的例子)。


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

解决方案

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

这篇关于如何将用户创建的连接传递给hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆