Navigation Menu

Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
actual transaction files
Browse files Browse the repository at this point in the history
  • Loading branch information
szimano committed Jul 29, 2010
1 parent 2191c25 commit b68a8ee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
@@ -0,0 +1,23 @@
package pl.softwaremill.cdiext.transaction;

import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;
import java.lang.annotation.*;

/**
* Changes current trancation timeout
* @author Adam Warski (adam at warski dot org)
*/
@Inherited
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TransactionTimeout {
/**
* New value of the transaction timeout in seconds
*
* @return Transaction timeout
*/
@Nonbinding int timeout();
}

@@ -0,0 +1,45 @@
package pl.softwaremill.cdiext.transaction;

import javax.annotation.Resource;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import java.io.Serializable;
import java.lang.annotation.Annotation;

/**
* An interceptor for the {@link Transactional} annotation.
* @author Adam Warski (adam at warski dot org)
* @link http://smokeandice.blogspot.com/2009/12/cdi-and-declarative-transactions.html
*/
@TransactionTimeout(timeout = 5)
@Interceptor
public class TransactionTimeoutInterceptor implements Serializable {
@Resource
private UserTransaction utx;

@AroundInvoke
public Object intercept(InvocationContext ic) throws Throwable {

// make sure the transaction is active
if (utx.getStatus() != Status.STATUS_ACTIVE) {
throw new RuntimeException("Transaction is not active and timeout cannot be set");
}

// check type
TransactionTimeout timeout = ic.getTarget().getClass().getAnnotation(TransactionTimeout.class);

// if not available check method
if (timeout == null) {
timeout = ic.getMethod().getAnnotation(TransactionTimeout.class);
}

// set the timeout
utx.setTransactionTimeout(timeout.timeout());

return ic.proceed();
}

}

0 comments on commit b68a8ee

Please sign in to comment.