Bom Dia Galera.
Creio ter encontrado um BUG no HibernateFilter
Bom estava olhando os historicos de alterações do SVN. E vi que na ultima versão mudaram o método aonde é controlada a transação
Irei postar só os métodos filter e afterConsequence
Notem que na revisão 374 a Transaction é feito no afterConsequence.
E na revisão 518 a Transaction é no método filter.
Eu creio que o correto seja mesmo no afterConsequence. Pois se der qualquer erro que seja em qualquer parte da requisição ele ira dar um roolback na transaction. E assim se der exceção em alguma parte o ExceptionFilter captura e nao continua do ponto aonde foi feito o chain.invoke(); No HibernateFilter. Sendo assim ele não vai dar nem um roolBack nem um commit.
HibernateFilter revisão 374
Code:
/**
* Sets the action to get an Hibernate Session using the <i>pull</i> model.
*/
public String filter(InvocationChain chain) throws Exception {
Action action = chain.getAction();
super.setInput(action.getInput());
action.setInput(this);
return chain.invoke();
}
public void afterConsequence(Action action, Consequence c,
boolean conseqExecuted, boolean actionExecuted, String result) {
if (transactional) {
Transaction trans = hibernateTransactionThreadLocal.get();
if (trans != null) {
hibernateTransactionThreadLocal.set(null);
removeValue(hibernateTransactionKey);
boolean shouldRollback = !actionExecuted || result == null || resultsForRollback.contains(result);
if (!shouldRollback) {
try {
trans.commit();
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to commit hibernate transaction!", e);
}
} else {
try {
trans.rollback();
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to rollback hibernate transaction!", e);
}
}
}
}
Session session = hibernateSessionThreadLocal.get();
if (session != null) {
hibernateSessionThreadLocal.set(null);
removeValue(hibernateSessionKey);
session.close();
}
}
HibernateFilter revisão 518
Code:
/**
* Sets the action to get an Hibernate Session using the <i>pull</i> model.
*/
public String filter(InvocationChain chain) throws Exception {
Action action = chain.getAction();
super.setInput(action.getInput());
action.setInput(this);
String result = chain.invoke();
// commit or rollback the transaction BEFORE consequence...
if (transactional) {
Transaction trans = hibernateTransactionThreadLocal.get();
if (trans != null) {
hibernateTransactionThreadLocal.set(null);
removeValue(hibernateTransactionKey);
boolean shouldRollback = result == null || resultsForRollback.contains(result);
if (!shouldRollback) {
try {
trans.commit();
} catch(Exception e) {
e.printStackTrace();
throw new FilterException("Unable to commit hibernate transaction!", e);
}
} else {
try {
trans.rollback();
} catch(Exception e) {
e.printStackTrace();
throw new FilterException("Unable to rollback hibernate transaction!", e);
}
}
}
}
return result;
}
public void afterConsequence(Action action, Consequence c,
boolean conseqExecuted, boolean actionExecuted, String result) {
Session session = hibernateSessionThreadLocal.get();
if (session != null) {
hibernateSessionThreadLocal.set(null);
removeValue(hibernateSessionKey);
session.close();
}
}