Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6865385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:02:25+00:00 2026-05-27T03:02:25+00:00

How can I set a transaction isolation level using Squeryl? For instance, right now

  • 0

How can I set a transaction isolation level using Squeryl?

For instance, right now I am using Postgresql and need serializable isolation for specific single transactions. I use both plain Squeryl and Squeryl-Record with the Lift web framework.

Others may of course need other isolation levels for other databases for entire sessions (rather than single transactions), so general answers are preferable.

Update:

I ended up with this modified version of Dave Whittaker’s code:

def transactionWith[T](isolation: Int)(block: => T): T =
  transaction {
    val connection = Session.currentSession.connection
    connection.rollback // isolation cannot be changed in the middle of a tx
    connection.setTransactionIsolation(isolation)
    block
  }

The thing is that if a transaction has already been started, you can’t change the isolation level. This was the case for me and without the rollback I would get:

org.postgresql.util.PSQLException: Cannot change transaction isolation level in the middle of a transaction.

As long as I am using transaction{} and not inTransaction{} I think doing an immediate rollback should do no harm.

The isolation level should be reset after transaction{} commits or rollbacks, but before the connection is returned to the connection pool. I’m not sure how to accomplish that. But in my case the c3p0 connection pool seems to reset the isolation level and every transaction{} starts with default isolation level even if I never clean them up myself.

The thing I’m not so happy with is the exception when there is a conflict. I would like to catch such an exception specifically and retry the transaction. But it’s just a generic runtime exception:

java.lang.RuntimeException: Exception while executing statement : ERROR: could not serialize access due to concurrent update

It wraps another exception which unfortunately also is generic (org.postgresql.util.PSQLException).

Not perfect, but it does the job until Squeryl hopefully gets support for transaction isolation. I am using the above code with Squeryl 0.9.4.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:02:25+00:00Added an answer on May 27, 2026 at 3:02 am

    Right now it would be a somewhat manual process. If you need it for the entire session then I suppose you could simply set the appropriate level in your SessionFactory, i.e.

    SessionFactory.concreteFactory = Some(()=> {
      val connection = java.sql.DriverManager.getConnection("...")
      connection.setTransactionIsolation(...)
      Session.create(connection, new PostgreSqlAdapter)
     })
    

    For a single transaction it would be a bit more difficult. You can access the current session with Session.currentSession or Session.currentSessionOption and you would have to set the isolation level before your transaction occurred and then set it back afterwards. Of course, it wouldn’t be too difficult to create your own function that does just that:

    def transactionWith(isolation: Int)(block: => T): T = {
      trasaction{
        val connection = Session.currentSession.connection
        val oldIsolation = connection.getTransactionIsolation()
        connection.setTransactionIsolation(isolation)
        try {
          block
        } finally {
          connection.setTransactionIsolation(oldIsolation)
        }
      }
    }
    

    Then you would use it like

    transactionWith(Connection.TRANSACTION_SERIALIZABLE){
       from(blablabla)(......)
    }
    

    I think that would work but a) I’m not entirely sure when the isolation level is supposed to be set, I’m assuming that setting it within the current transaction before executing any other statements will work and b) I haven’t tried to compile the above so there may be syntactic errors. Anyway, I think it will give you the general idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I set the isolation level of a transaction to 'SERIALIZABLE' in PHP
How do I set the global transaction isolation level for a postgres data source.
ALTER PROCEDURE [Lending].[uspHMDALarIncomeGet] (@ApplicationId int) AS BEGIN SET NOCOUNT ON SET TRANSACTION ISOLATION LEVEL
This: use test; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; BEGIN TRANSACTION; EXEC sp_RENAME 'table1.asd'
i am using Hibernate with oracle . What will be default isolation level if
Should I run ALTER DATABASE DbName SET ALLOW_SNAPSHOT_ISOLATION OFF if snapshot transaction (TX) isolation
I am not able to find a way to set TransactionIsolation in ejb. Can
I can set the PHP include path in the php.ini : include_path = /path/to/site/includes/
I can set data in JTable constructor, and then user can change this data
I can set the minimum version required (for example XP SP3) in Inno-Setup by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.