Multiple updates with optimistic trait
Squeryl throws a StaleUpdateException when is used with multiples updates of the same entity inside a transaction. Class to reproduce:
package org.databench.frameworks
import org.squeryl.Schema
import org.squeryl.KeyedEntity
import org.squeryl.Optimistic
import org.squeryl.SessionFactory
import org.squeryl.Session
import java.sql.DriverManager
import org.squeryl.adapters.MySQLAdapter
import org.squeryl.PrimitiveTypeMode._
class TestEntity(val id: Long)
extends KeyedEntity[Long]
with Optimistic {
def this() = this(-1)
var counter = 0
}
object TestSchema extends Schema {
val entities = table[TestEntity]("TestEntity")
}
object SquerylOptimisticLockMultipleUpdateBug extends App {
def createConnection = {
Class.forName("com.mysql.jdbc.Driver")
val con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/squeryl", "root", "")
con.setAutoCommit(false)
con
}
SessionFactory.concreteFactory = Some(() =>
Session.create(
createConnection,
new MySQLAdapter))
transaction {
TestSchema.drop
TestSchema.create
}
transaction {
TestSchema.entities.insert(new TestEntity())
}
transaction {
val testEntity = TestSchema.entities.head
testEntity.counter += 1
TestSchema.entities.update(testEntity)
testEntity.counter += 1
TestSchema.entities.update(testEntity)
}
}
package org.databench.frameworks
import org.squeryl.Schema
import org.squeryl.KeyedEntity
import org.squeryl.Optimistic
import org.squeryl.SessionFactory
import org.squeryl.Session
import java.sql.DriverManager
import org.squeryl.adapters.MySQLAdapter
import org.squeryl.PrimitiveTypeMode._
class TestEntity(val id: Long)
extends KeyedEntity[Long]
with Optimistic {
def this() = this(-1)
var counter = 0
}
object TestSchema extends Schema {
val entities = table[TestEntity]("TestEntity")
}
object SquerylOptimisticLockMultipleUpdateBug extends App {
def createConnection = {
Class.forName("com.mysql.jdbc.Driver")
val con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/squeryl", "root", "")
con.setAutoCommit(false)
con
}
SessionFactory.concreteFactory = Some(() =>
Session.create(
createConnection,
new MySQLAdapter))
transaction {
TestSchema.drop
TestSchema.create
}
transaction {
TestSchema.entities.insert(new TestEntity())
}
transaction {
val testEntity = TestSchema.entities.head
testEntity.counter += 1
TestSchema.entities.update(testEntity)
testEntity.counter += 1
TestSchema.entities.update(testEntity)
}
}
Leave a comment