derekv
derekv

Reputation: 3141

Is it possible to treat a table as read only in hibernate?

I have some tables for which I have no reason to ever update from within the application, so I would like to prevent it from happening even accidentally by a bug somewhere else.

I see the @Immutable annotation, but it looks like that will still allow insertions and deletions. I'd like to full on treat the whole table (not just each entity) as written in stone. Is there an easy way to accomplish this? Or do I misunderstand the documentation on @Immutable?

If an example is needed, lets say that there is a table with MONTH table, and a Month entity, and a APPOINTMENT table with a Appointment entity associated. I would never want to delete or insert a row into month.

Upvotes: 6

Views: 5732

Answers (2)

instanceOfObject
instanceOfObject

Reputation: 2984

One way is to use READ_ONLY transactions by putting @Transactional(readOnly=true) over the method.

http://www.codeinstructions.com/2009/04/read-only-transactions-with-spring-and.html.

Another method : As far as isolation levels go, if your table is never inserted or updated you can use the READ_UNCOMMITTED isolation level, which allows dirty reads, non-repeatable reads and phantom reads. However none of that matters since the data is never changing.

You can look at the different isolation levels and effects of each in the spring javadocs (http://docs.huihoo.com/javadoc/spring/2.5/org/springframework/transaction/annotation/Isolation.html)

This will free up locks on rows the most and will give you the best performance, at least as far as locking goes

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340873

Have you tried read only caching strategy:

If your application needs to read, but not modify, instances of a persistent class, a read-only cache can be used. This is the simplest and optimal performing strategy. It is even safe for use in a cluster.

From: 19.2.2. Strategy: read only

Upvotes: 3

Related Questions