So, what the hell happens when you try and save changes to an @Version’d entity when another thread saves it’s changes before you?

Imagine you’re setup with a JPA 2 environment, using Spring Data JPA and Hibernate is your JPA vendor. You have a UserRepository interface which extends JpaRepository and therefore a Spring generated userRepository bean for all your user DAO needs. You also have a User domain object with a field annotated with @Version.

FYI, your find, save, delete etc. methods in your userRepository are automatically @Transactional. Thank you, Spring.

Ok, lets formalise this a bit:

Thread 1 finds User record with ID 1
Thread 2 finds User record with ID 1
Thread 2 changes the email address field
Thread 2 saves the changes to their version of the user object
Thread 1 changes the email address field
Thread 1 saves the changes to their version of the user object

At the point where thread 1 saves changes, it’ll check the version field of the user object and also the version of the user object in the database. They’ll be different, and a HibernateOptimisticLockingFailureException will be thrown.

Note also that the exact same thing happens if either thread 1 or thread 2 had deleted the object. So, all well and good huh? Check this out for sensible:

Thread 1 finds User record with ID 1
Thread 2 finds User record with ID 1
Thread 2 changes the email address field
Thread 2 saves the changes to their version of the user object
Thread 1 changes nothing, but then saves their version of the user object

What happens here? I originally thought thread 1 would throw an optimistic locking exception. Except, it doesn’t – Spring/JPA/whoever is in charge back there is smart enough to know that nothing changed, so it doesn’t save anything, and you end up with thread 2′s changes as you wanted. Clever girl.

Woes with Scala native types, Spring, JPA and FreeMarker

I’m working on a project using Scala, Spring, JPA and FreeMarker (as you probably guessed from the title of the blog post). I love working with Scala because for the most part, it makes my code more succinct and it is really exciting to work with. I mean, there are obviously other reasons but those are two big ones.

What I don’t like is having to convert between Scala and Java equivalents, but sadly it is unavoidable. The scala.collection.JavaConverters and scala.collection.JavaConversions objects make this a lot easier through implicit and explicit conversions but it does make you wonder what the performance hit is…

I mostly choose to ignore that there is a performance hit – performance problems are a happy problem anyway right?…and at the moment the benefits of being able to write code in Scala greatly outweigh any of my worries.

I use Scala types wherever possible in this project i.e. List, BigDecimal etc. but sometimes it’s just not possible. My domain objects have to use Java types because the JPA vendor doesn’t know how to translate the Scala types into SQL column types. The same goes for objects that are going to be passed to FreeMarker. FreeMarker cannot iterate over a Scala list using the <#list scalaList as item> syntax as you normally would since it doesn’t “know” what a Scala List is. As far as FreeMarker is concerned a Scala List is a java.lang.Object (see scala-lang.org/node/71).

My repositories are generated dynamically by Spring. Basically you define an interface and Spring magically generates a class that extends Spring’s JpaRepository and implements your interface. There is no two ways about it, I want this magic, but it means that the “interfaces” (which are coded as traits in Scala) have to return Java lists for findAll or findBy operations – boo.

Overall it isn’t actually that much of a pain, so I don’t know why I’m complaining about it. It is just something that slightly tarnishes the code, but I guess I can live with it. After all I did decide to use a bunch of Java frameworks with my Scala code. Why didn’t I just use Scala frameworks? Well, mostly because I’m pretty mad, but I’m not completely insane. I figured switching from Java to Scala was a big enough brain realignment without changing everything else I’m used to using as well.