This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Hibernate and Jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate and Jdbc" Watch "Hibernate and Jdbc" New topic
Author

Hibernate and Jdbc

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
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.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes you can. But why would you want to?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Nov 25, 2003
Posts: 315
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Nov 25, 2003
Posts: 315
Thanks a ton. will try and let you know.
 
 
subject: Hibernate and Jdbc