This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
cdi/src/main/java/pl/softwaremill/cdiext/transaction/TransactionTimeout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
45 changes: 45 additions & 0 deletions
45
cdi/src/main/java/pl/softwaremill/cdiext/transaction/TransactionTimeoutInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |