Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Only Database Connection with Hibernate

Is it possible to use Hibernate and connect to a database with a read only connection? I will be working on a project that will require connecting to an existing database, pulling data from it, and doing some complex data manipulation in the application. Throughout all of this I can`t change anything in the database, hence the read only connection requirement.

My first thought was to pull the data from the database using Hibernate so that I can have ready made Java objects represent the data, however, I can`t seem to find any information on how to force Hibernate to use a read only database connection ... I have a feeling this might actually be impossible, but I want to find out from others before I pursue other ideas.

like image 281
Harry Muscle Avatar asked Mar 01 '26 21:03

Harry Muscle


2 Answers

I don't have enough reputation to comment, apparently :(

But responding to your comment about the cases where Hibernate may still write back to the DB, you could manually detach the object from your persistence context, after which Hibernate would cease caring about the state of the object & whether its been modified.

like image 119
Angelina Avatar answered Mar 03 '26 11:03

Angelina


You can use: Session.setDefaultReadOnly( true );

http://docs.jboss.org/hibernate/orm/3.5/reference/en-US/html/readonly.html#readonly-api-loaddefault

like image 41
janek Avatar answered Mar 03 '26 10:03

janek