开发者

plain jdbc and hibernate

I have a project running live for couple of years and it uses plain JDBC.

Now I have to implement a small module for which I am considering Hibernate. I configured Hibernate (hibernate-cfg.xml) and it's up and running.

So I have DB properties defined in two places oracle-ds.xml and hibernate-cfg.xml

  1. Is it OK to mix JDBC and Hibernate in a single application, if not what are the issues?
  2. Do I need to configure "connection pool" in Hibernate configuration and also in oracle-ds.xml (JBoss application server)?
  3. I thought of configurin开发者_运维百科g hibernate using JNDI but that needs mbean, HAR that's extra work and this new module is going to be used rarely. OR Should I make it as an JBoss service so that connection pooling is handled by the container?
  4. I can use plain JDBC but the team wants to try Hibernate and if everything works fine, we are looking into using Hibernate full time for next project.


Is that OK to mix jdbc, hibernate in a single application, if not what are the issues?

It's doable but you need to take some precautions:

  • You can't easily mix JDBC handled POJOs in a graph of Hibernate handled objects (so try to use Hibernate for the deepest objects in a graph or for fully Hibernatized graphs).
  • I guess you have some concurrency mechanism, use the same for JDBC and Hibernate (i.e. increment the same version column from JDBC and Hibernate).
  • If you configure Hibernate to use the 2nd level cache, Hibernate won't be aware of changes made without using its API (so you might have to implement some evict mechanism).

Do I need to configure "connection pool" in hibernate configuration and also in oracle-ds.xml

I would configure Hibernate to use the app server connection pool (see the Hibernate Datasource Properties).

I thought of configuring hibernate using JNDI but that needs mbean, HAR thats extra work and this new module is going to be used rarely. OR Should I make it as an jboss service so that connection pooling is handled by container?

You don't need to use a JBoss service or to use a HAR to use connection pooliong, see the above link. And I wouldn't bother with that for now, this is very low priority stuff in my opinion. First, bootstrap a first prototype, see how it fit with your existing code, what needs to be changed, etc.


We've mixed the two in the same application with no problems. I set the server to manage the connection pool so they end up getting shared between the JDBC code and hibernate code.

One thing you do have to keep in mind is hibernate caches the data in memory, so if you change a hibernate table directly, the application will still keep seeing the old data that's in the cache. If you turn off the cache, it takes a big performance hit. It saves you coding but you lose a lot in flexibility. It's difficult to integrate with other applications through the database and do things like replication between sites.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜