Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So you've encapsulated the transaction demarcation logic in one place and use dynamic proxy to enhance existing services with transaction management and reduce boilerplate code, right?</p> <p>The sounds a rather OK to me. Actually what containers such as Spring, or EJB do when we speak of <a href="http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative" rel="nofollow noreferrer"><strong>declarative transaction demarcation</strong></a> is very similar. Implementation-wise, you can do it with dynamic proxy, or byte code instrumentation, or even use AspectJ. I did something very similar once for a tiny testing framework once. Here is a <a href="http://www.ewernli.com/web/guest/45" rel="nofollow noreferrer">blog post</a> about it.</p> <p>The tricky parts that I see are:</p> <p>1) <strong><em>Rollback only</em></strong>. As per JPA spec, an entity transaction can be flagged as "<a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityTransaction.html" rel="nofollow noreferrer">rollback only</a>". Such a transaction can never commit. So I feel like you should check that between these two lines:</p> <pre><code>result = method.invoke(object, args); et.commit(); </code></pre> <p>2) <strong><em>Re-entrancy</em></strong>. Most system that have declarative transaction implement a semantics in which a transaction is started only if there isn't one already active (See "Required" in this list of <a href="http://openejb.apache.org/3.0/transaction-annotations.html" rel="nofollow noreferrer">EJB annotations</a>). Looks like you should maybe check with <code>isActive</code> that in your logic.</p> <p>3) <strong><em>Exception handling</em></strong>. Be very careful with the exception propagation in dynamic proxy. The proxy is supposed to be transparent for the client as much as possible. If an exception other than <code>DAOException</code> leaks out of the DAO, the proxy will transform it into a <code>RuntimeException</code>. Doesn't sound optimal to me. Also don't confuse the exception because <code>invoke</code> failed, and the exception wrapped by the invocation, that I think you should re-throw as-is: </p> <pre><code>catch ( InvocationTargetException e ) { Throwable nested = e.getTargetException(); throw nested; } </code></pre> <p><strong>Conclusion</strong>: the idea to use dynamic proxy in this scenario sounds OK to me. But I suspect there are a few stuffs to double-check in your code (I don't remember all the details of the JPA specs and exception handling with dynamic proxy, but there are some tricky cases). This kind of code can hide subtle bugs, so it's worth taking time to make it bullet-proof.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload