Skip to content

Commit

Permalink
fixes Bug mit MySQL Deadlock beim Mutli-File-Upload: Die Subquery im …
Browse files Browse the repository at this point in the history
…Insert hat bei paralleler Verarbeitung Probleme gemacht
  • Loading branch information
StefanPenndorf committed Oct 22, 2013
1 parent 1a99e99 commit fa07f92
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions app/model/Fotoalbum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,40 @@ class PersistenterFotoalbenVerwalter extends FotoalbenVerwalter {
}

def speichereFotoFuerBenutzer(foto: Array[Byte], besitzer: Benutzer) {
DB.withConnection { implicit connection =>
SQL(
val insertedId = DB.withTransaction { implicit connection =>
val pos = SQL(
"""SELECT COUNT(*)+1 FROM
( SELECT id FROM fotos f
WHERE f.besitzer={besitzerid}
ORDER BY f.id
FOR UPDATE ) r """
).on(
'besitzerid -> besitzer.id
).as(scalar[Long].single)

val insertId = SQL(
"""
INSERT INTO fotos (besitzer, foto, position) VALUES
( {besitzerid}, {foto}, (SELECT COUNT(*)+1 FROM fotos f2 WHERE f2.besitzer={besitzerid}) )
( {besitzerid}, {foto}, {pos} )
"""
).on(
'besitzerid -> besitzer.id,
'foto -> foto
).executeUpdate()
'foto -> new Array[Byte](2),
'pos -> pos
).executeInsert()

insertId
}

DB.withConnection { implicit connection =>
SQL(
"""
UPDATE fotos SET foto = {foto} WHERE id = {id}
"""
).on(
'id -> insertedId,
'foto -> foto
).executeUpdate()
}
}

Expand Down

0 comments on commit fa07f92

Please sign in to comment.