static int taskId = 0;
private static Runnable runnableInNewThread(final Scriptable scope, Object[] args, String name) {
if(args.length == 0 || !(args[0] instanceof Callable))
throw ScriptRuntime.constructError("TypeError", "The first parameter of " + name + " must be a function");
final Callable function = (Callable) args[0];
TimerTask timeoutTask;
final int thisTaskId = taskId++;
final Object user = UserSecurity.currentUser();
IndividualRequest request = Client.getCurrentObjectResponse();
final Client session = request == null ? null : Client.getCurrentObjectResponse().getConnection();
return new Runnable(){
public void run() {
try{
if(session != null)
session.adoptThread(Thread.currentThread());
UserSecurity.registerThisThread(user);
Transaction.startTransaction();
function.call(PersevereContextFactory.getContext(), scope, null, new Object[]{});
Transaction.currentTransaction().commit();
}catch(Throwable e){
if(e instanceof RhinoException)
log.warn(((RhinoException)e).details() + '\n' + ((RhinoException)e).getScriptStackTrace());
else