Do jobs use transactions for database updates?

500 views
Skip to first unread message

msanitariusz

unread,
Dec 30, 2009, 4:50:21 AM12/30/09
to play-framework
Do jobs use transactions for database updates?
I've noticed that my database is updated only when the running job is
finished, not for every model.save(), what would seem natural to me.
How to enforce database update for every model.save()?
I do not use transactions in my application at all.
Here's the job source code:

package jobs;

import play.jobs.*;
import models.*;
import java.util.*;
import calculator.*;
import play.*;

@On("0 0 12 * * ?")
public class UpdateCounters extends Job {
public void doJob() {
List<Employee> empList = Employee.find("long and boring
SQL :)").fetch();
if (empList.isEmpty() != true) {
Iterator<Employee> empIterator = empList.iterator();
while (empIterator.hasNext()) {
Employee emp = empIterator.next();
CounterCalculator cc = new CounterCalculator(); //
magic stuff :) from calculator package
String[] calculations = cc.search(emp.id);
if (!calculations[0].isEmpty() && !calculations
[0].isEmpty()) {
emp.counter = calculations[0];
emp.treshold = calculations[1];
emp.save(); // no changes to the database
until UpdateCounders job finishes
}
}
}
}
}

Guillaume Bort

unread,
Dec 30, 2009, 5:11:37 AM12/30/09
to play-fr...@googlegroups.com
Yes a job run as a plain invocation, so with the JPA plugin enabled it
run within a transaction.

There is something bad with this transaction ?

> --
>
> You received this message because you are subscribed to the Google
> Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en
> .
>
>

Tomas

unread,
Dec 30, 2009, 5:19:46 AM12/30/09
to play-framework
there is a hacky way to close and open new transaction that would
persist your data:
entity.save()
Entity.em().getTransaction().commit();
Entity.em().getTransaction().begin();

Guillaume Bort

unread,
Dec 30, 2009, 5:46:19 AM12/30/09
to play-fr...@googlegroups.com
Another workaround is to split your process into several small jobs:

Something like:

@On("0 0 12 * * ?")
public class UpdateCounters extends Job {
public void doJob() {
List<Employee> empList = Employee.find("long and boring
SQL :)").fetch();

for(Employee emp : empList) {
new UpdateEmployeeJob(emp).now().get();
}
}
}

This way each UpdateEmployeeJob will have its own transaction;

Thibaut

unread,
Feb 21, 2010, 6:28:49 PM2/21/10
to play-framework
Is there a nicer solution to this?

I have a background job which will never stop (running in an endless
loop), thus the objects will never be commited when I call .save();

Guillaume Bort

unread,
Feb 22, 2010, 5:40:57 AM2/22/10
to play-fr...@googlegroups.com
You can't do that. A job is a play invocation and cannot be infinite.
You have to split your work into several small invocations.

Reply all
Reply to author
Forward
0 new messages