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
75 additions
and
0 deletions.
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
impl/src/main/java/org/jboss/seam/transaction/RollbackFilter.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,69 @@ | ||
package org.jboss.seam.transaction; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.inject.Inject; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.annotation.WebFilter; | ||
import javax.transaction.Status; | ||
import javax.transaction.SystemException; | ||
import javax.transaction.UserTransaction; | ||
|
||
/** | ||
* This filter rolls back the current transaction if it is still active. | ||
* | ||
* @author Nicklas Karlsson | ||
* | ||
*/ | ||
@WebFilter(value = "/*") | ||
public class RollbackFilter implements Filter | ||
{ | ||
@Inject | ||
private UserTransaction userTransaction; | ||
|
||
@Override | ||
public void destroy() | ||
{ | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException | ||
{ | ||
try | ||
{ | ||
rollbackTransactionIfActive(); | ||
chain.doFilter(request, response); | ||
} | ||
finally | ||
{ | ||
rollbackTransactionIfActive(); | ||
} | ||
} | ||
|
||
private void rollbackTransactionIfActive() | ||
{ | ||
try | ||
{ | ||
if (userTransaction.getStatus() == Status.STATUS_ACTIVE) | ||
{ | ||
userTransaction.rollback(); | ||
} | ||
} | ||
catch (SystemException e) | ||
{ | ||
// so not? | ||
} | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig config) throws ServletException | ||
{ | ||
} | ||
|
||
} | ||
|