|
From: <php...@li...> - 2009-09-23 21:59:53
|
> Would it be feasible to create a generic callback interface in Java
> and then use java_closure() on the PHP side to create the equivalent
> of an anonymous inner class which implements the callback interface?
> I could then pass this to a method on the Java side that executes the
> callback within the context of a transaction, and additionally
> handles and wraps all exceptions with a type that it explicitly
> declares. Would this solve the issue without the performance
> penalties that the JAVA_PREFER_VALUES option would? Or am I totally
> on the wrong track here?
Sorry to followup to my own post here, but I tried this out and it does
work. The code I have on the Java side is roughly equivalent to the
following:
public static void runInTransaction(Callback callback) throws
CallbackException {
SessionFactory factory = getSessionFactory();
try {
factory.getCurrentSession().beginTransaction();
callback.execute();
factory.getCurrentSession().getTransaction().commit();
} catch (RuntimeException e) {
factory.getCurrentSession().getTransaction().rollback();
throw new CallbackException(e);
}
}
>From the PHP side:
class MyCallback {
function execute() {
//arbitrary code
}
}
$callback = java_closure(new MyCallback(), null, new Java("Callback"));
try {
java(...)->runInTransaction($callback);
} catch (JavaException $ex) {
//handle exception
}
This seems to work. Does this sidestep the exception handling issue? Does
it significantly impact performance versus ensuring that all methods in the
arbitrary code declare all exceptions that they throw?
Thanks in advance...
Michael
|