• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Hibernate and Jdbc

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can i connect to a db using Hibernate and JDBC from a single Java class in sequential order.

1. method for JDBC database connection open and close.
2. method fopr Hibernate connection open and close.

I don't any issue in it, if connections are opened and closed properly.

Please provide more inputs, if it can cause the db to hang.

Thanks,
Neeraj.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can. But why would you want to?
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks paul.

Our application is built using JDBC calls for DB operations. We have included a third party jar file which will invoke an ejb deployed on a remote location. The ejb call will return results. Those results are saved by classes included in the third party jar. third party jar file is using hibernate to save the results in db. same db is accessed by our application using JDBC.


This approach was implemented in another project of my client and it was causing the database to hang.

What I feel is that those application might be having issue with the connections left opened either from hibernate/JDBC calls.

If you handle connections properly, then it should not have any issues.

thanks again,
Neeraj
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This approach was implemented in another project of my client and it was causing the database to hang.


If the operations are as described, e.g. JDBC opens connection, does stuff, closes connection - Hibernate opens session, does stuff, closes session, the only issue you will need to worry about is the transaction isolation used. Assuming both operations occur in the same User Transaction (?) you are probably using two connections. If these operations could hit the same tables in the database you'll possibly hit row or page locks depending on your database. Event if you don't hit a lock you are still running the risk of dirty data.

What database are you using? Do both DB operations hit the same database? An if so, do they affect the same tables?
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

We are using sql server 2005.

Step 1 - JDBC calls a.) open connection b.) get application number c.) closeconnection

step 2 - Invokde ejb with application number as parameter. Ejb will get results. open hibernate connection - save results- close connection.

step 3 - open jdbc connection - read results save in step 2 and update results in the tables- close jdbc connection.

Both JDBC and hibernate will access same database and same set of tables but at different steps. [at step 2 and step 3]

can it still create dirty data.

what I am suspecting is the timing when hibernate will commit the data into database. can there be a possibility, that hibernate keeps the data in cache before the step 3 executes and try to get the data from the tables.

Thanks again,
Neeraj.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do your transaction(s) start and stop? Is the whole operation in one transaction, or is there a transaction per step?


what I am suspecting is the timing when hibernate will commit the data into database. can there be a possibility, that hibernate keeps the data in cache before the step 3 executes and try to get the data from the tables.


Hibernate does not do this after the session has been flushed. What would cause an issue is if a transaction has not been commited.

SQL Serevr implements transaction isolation using locks. Because of this it has a concept of a shared read lock (used for select statements). A side effect is that updates cannot happen till these locks have been released, which is why you often see blocking in SQL Server. The single most common cause of such a block is a client failing to commit or rollback a transaction (in which case the lock cannot be release). Try executeing sp_who2, see what is blocking and what state they are in. If it is "awaiting command" its a good bet your client still has a transaction open.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton. will try and let you know.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic