Rafael_Guerreiro 13 de jun. de 2011
Se você debugar, qual exception acontece?
CommanderShepard 13 de jun. de 2011
tudo bom Rafael
abaixo um print do Inspect do Eclipse com o objeto “Exception exception”
Rafael_Guerreiro 13 de jun. de 2011
Ele não está dando um StaleObjectStateException…
Ou o RollbackException extends StaleObjectStateException?
CommanderShepard 13 de jun. de 2011
o StaleObjectStateException acontece
ele é printado na stack trace, o funcionamento dele tá ok (estamos testando a concorrência e até agora tudo ok)
o problema tá sendo pegar ele no Interceptor pra jogar um erro legal pro usuário
senão me engano a árvore do StaleObjectStateException é “StaleObjectStateException->StaleStateException->HibernateException->RuntimeException->…” e o RollbackException é “javax.algumacoisa”
Lucas_Cavalcanti 13 de jun. de 2011
o problema é que antes de chegar em vc está passando pelo interceptor de transações, e ele tá tentando dar o commit de uma transação que já estava marcada como rollback only.
use a última versão do VRaptor e anote seu interceptor da StaleObject com:
@Intercepts ( after = HibernateTransactionInterceptor . class )
assim a exception vai passar antes (na volta) pelo seu interceptor
CommanderShepard 13 de jun. de 2011
tudo bom Lucas
implementando sua sugestão está gerando uma exceção
11 : 46 : 02 , 433 ERROR [ [/meuprojeto ] ] Unhandled exception occurred whilst decorating page
org . springframework . beans . factory . NoSuchBeanDefinitionException : No unique bean of type [ br.com.caelum.vraptor.util.hibernate.HibernateTransactionInterceptor ] is defined : expected single bean but found 0 :
at org . springframework . beans . factory . support . DefaultListableBeanFactory . getBean ( DefaultListableBeanFactory . java : 269 )
at org . springframework . context . support . AbstractApplicationContext . getBean ( AbstractApplicationContext . java : 1083 )
at br . com . caelum . vraptor . ioc . spring . SpringBasedContainer . instanceFor ( SpringBasedContainer . java : 86 )
at br . com . caelum . vraptor . core . ToInstantiateInterceptorHandler . execute ( ToInstantiateInterceptorHandler . java : 47 )
at br . com . caelum . vraptor . core . DefaultInterceptorStack . next ( DefaultInterceptorStack . java : 54 )
at br . com . caelum . vraptor . interceptor . ResourceLookupInterceptor . intercept ( ResourceLookupInterceptor . java : 69 )
at br . com . caelum . vraptor . core . ToInstantiateInterceptorHandler . execute ( ToInstantiateInterceptorHandler . java : 54 )
at br . com . caelum . vraptor . core . DefaultInterceptorStack . next ( DefaultInterceptorStack . java : 54 )
at br . com . caelum . vraptor . core . EnhancedRequestExecution . execute ( EnhancedRequestExecution . java : 23 )
at br . com . caelum . vraptor . VRaptor $ 1. insideRequest ( VRaptor . java : 92 )
at br . com . caelum . vraptor . ioc . spring . SpringProvider . provideForRequest ( SpringProvider . java : 58 )
at br . com . caelum . vraptor . VRaptor . doFilter ( VRaptor . java : 89 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 368 )
at org . springframework . security . web . access . intercept . FilterSecurityInterceptor . invoke ( FilterSecurityInterceptor . java : 109 )
at org . springframework . security . web . access . intercept . FilterSecurityInterceptor . doFilter ( FilterSecurityInterceptor . java : 83 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . access . ExceptionTranslationFilter . doFilter ( ExceptionTranslationFilter . java : 97 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . session . SessionManagementFilter . doFilter ( SessionManagementFilter . java : 100 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . AnonymousAuthenticationFilter . doFilter ( AnonymousAuthenticationFilter . java : 78 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . servletapi . SecurityContextHolderAwareRequestFilter . doFilter ( SecurityContextHolderAwareRequestFilter . java : 54 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . savedrequest . RequestCacheAwareFilter . doFilter ( RequestCacheAwareFilter . java : 35 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . www . BasicAuthenticationFilter . doFilter ( BasicAuthenticationFilter . java : 177 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . AbstractAuthenticationProcessingFilter . doFilter ( AbstractAuthenticationProcessingFilter . java : 187 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . logout . LogoutFilter . doFilter ( LogoutFilter . java : 105 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . context . SecurityContextPersistenceFilter . doFilter ( SecurityContextPersistenceFilter . java : 79 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . FilterChainProxy . doFilter ( FilterChainProxy . java : 169 )
at org . springframework . web . filter . DelegatingFilterProxy . invokeDelegate ( DelegatingFilterProxy . java : 237 )
at org . springframework . web . filter . DelegatingFilterProxy . doFilter ( DelegatingFilterProxy . java : 167 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at com . opensymphony . sitemesh . webapp . SiteMeshFilter . obtainContent ( SiteMeshFilter . java : 129 )
at com . opensymphony . sitemesh . webapp . SiteMeshFilter . doFilter ( SiteMeshFilter . java : 77 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at org . jboss . web . tomcat . filters . ReplyHeaderFilter . doFilter ( ReplyHeaderFilter . java : 96 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at org . apache . catalina . core . StandardWrapperValve . invoke ( StandardWrapperValve . java : 235 )
at org . apache . catalina . core . StandardContextValve . invoke ( StandardContextValve . java : 191 )
at org . jboss . web . tomcat . security . SecurityAssociationValve . invoke ( SecurityAssociationValve . java : 190 )
at org . jboss . web . tomcat . security . JaccContextValve . invoke ( JaccContextValve . java : 92 )
at org . jboss . web . tomcat . security . SecurityContextEstablishmentValve . process ( SecurityContextEstablishmentValve . java : 126 )
at org . jboss . web . tomcat . security . SecurityContextEstablishmentValve . invoke ( SecurityContextEstablishmentValve . java : 70 )
at org . apache . catalina . core . StandardHostValve . invoke ( StandardHostValve . java : 127 )
at org . apache . catalina . valves . ErrorReportValve . invoke ( ErrorReportValve . java : 102 )
at org . jboss . web . tomcat . service . jca . CachedConnectionValve . invoke ( CachedConnectionValve . java : 158 )
at org . apache . catalina . core . StandardEngineValve . invoke ( StandardEngineValve . java : 109 )
at org . apache . catalina . connector . CoyoteAdapter . service ( CoyoteAdapter . java : 330 )
at org . apache . coyote . http11 . Http11Processor . process ( Http11Processor . java : 829 )
at org . apache . coyote . http11 . Http11Protocol $ Http11ConnectionHandler . process ( Http11Protocol . java : 598 )
at org . apache . tomcat . util . net . JIoEndpoint $ Worker . run ( JIoEndpoint . java : 447 )
at java . lang . Thread . run ( Thread . java : 662 )
11 : 46 : 02 , 434 ERROR [ [default ] ] Servlet . service () for servlet default threw exception
org . springframework . beans . factory . NoSuchBeanDefinitionException : No unique bean of type [ br.com.caelum.vraptor.util.hibernate.HibernateTransactionInterceptor ] is defined : expected single bean but found 0 :
at org . springframework . beans . factory . support . DefaultListableBeanFactory . getBean ( DefaultListableBeanFactory . java : 269 )
at org . springframework . context . support . AbstractApplicationContext . getBean ( AbstractApplicationContext . java : 1083 )
at br . com . caelum . vraptor . ioc . spring . SpringBasedContainer . instanceFor ( SpringBasedContainer . java : 86 )
at br . com . caelum . vraptor . core . ToInstantiateInterceptorHandler . execute ( ToInstantiateInterceptorHandler . java : 47 )
at br . com . caelum . vraptor . core . DefaultInterceptorStack . next ( DefaultInterceptorStack . java : 54 )
at br . com . caelum . vraptor . interceptor . ResourceLookupInterceptor . intercept ( ResourceLookupInterceptor . java : 69 )
at br . com . caelum . vraptor . core . ToInstantiateInterceptorHandler . execute ( ToInstantiateInterceptorHandler . java : 54 )
at br . com . caelum . vraptor . core . DefaultInterceptorStack . next ( DefaultInterceptorStack . java : 54 )
at br . com . caelum . vraptor . core . EnhancedRequestExecution . execute ( EnhancedRequestExecution . java : 23 )
at br . com . caelum . vraptor . VRaptor $ 1. insideRequest ( VRaptor . java : 92 )
at br . com . caelum . vraptor . ioc . spring . SpringProvider . provideForRequest ( SpringProvider . java : 58 )
at br . com . caelum . vraptor . VRaptor . doFilter ( VRaptor . java : 89 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 368 )
at org . springframework . security . web . access . intercept . FilterSecurityInterceptor . invoke ( FilterSecurityInterceptor . java : 109 )
at org . springframework . security . web . access . intercept . FilterSecurityInterceptor . doFilter ( FilterSecurityInterceptor . java : 83 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . access . ExceptionTranslationFilter . doFilter ( ExceptionTranslationFilter . java : 97 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . session . SessionManagementFilter . doFilter ( SessionManagementFilter . java : 100 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . AnonymousAuthenticationFilter . doFilter ( AnonymousAuthenticationFilter . java : 78 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . servletapi . SecurityContextHolderAwareRequestFilter . doFilter ( SecurityContextHolderAwareRequestFilter . java : 54 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . savedrequest . RequestCacheAwareFilter . doFilter ( RequestCacheAwareFilter . java : 35 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . www . BasicAuthenticationFilter . doFilter ( BasicAuthenticationFilter . java : 177 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . AbstractAuthenticationProcessingFilter . doFilter ( AbstractAuthenticationProcessingFilter . java : 187 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . authentication . logout . LogoutFilter . doFilter ( LogoutFilter . java : 105 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . context . SecurityContextPersistenceFilter . doFilter ( SecurityContextPersistenceFilter . java : 79 )
at org . springframework . security . web . FilterChainProxy $ VirtualFilterChain . doFilter ( FilterChainProxy . java : 380 )
at org . springframework . security . web . FilterChainProxy . doFilter ( FilterChainProxy . java : 169 )
at org . springframework . web . filter . DelegatingFilterProxy . invokeDelegate ( DelegatingFilterProxy . java : 237 )
at org . springframework . web . filter . DelegatingFilterProxy . doFilter ( DelegatingFilterProxy . java : 167 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at com . opensymphony . sitemesh . webapp . SiteMeshFilter . obtainContent ( SiteMeshFilter . java : 129 )
at com . opensymphony . sitemesh . webapp . SiteMeshFilter . doFilter ( SiteMeshFilter . java : 77 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at org . jboss . web . tomcat . filters . ReplyHeaderFilter . doFilter ( ReplyHeaderFilter . java : 96 )
at org . apache . catalina . core . ApplicationFilterChain . internalDoFilter ( ApplicationFilterChain . java : 235 )
at org . apache . catalina . core . ApplicationFilterChain . doFilter ( ApplicationFilterChain . java : 206 )
at org . apache . catalina . core . StandardWrapperValve . invoke ( StandardWrapperValve . java : 235 )
at org . apache . catalina . core . StandardContextValve . invoke ( StandardContextValve . java : 191 )
at org . jboss . web . tomcat . security . SecurityAssociationValve . invoke ( SecurityAssociationValve . java : 190 )
at org . jboss . web . tomcat . security . JaccContextValve . invoke ( JaccContextValve . java : 92 )
at org . jboss . web . tomcat . security . SecurityContextEstablishmentValve . process ( SecurityContextEstablishmentValve . java : 126 )
at org . jboss . web . tomcat . security . SecurityContextEstablishmentValve . invoke ( SecurityContextEstablishmentValve . java : 70 )
at org . apache . catalina . core . StandardHostValve . invoke ( StandardHostValve . java : 127 )
at org . apache . catalina . valves . ErrorReportValve . invoke ( ErrorReportValve . java : 102 )
at org . jboss . web . tomcat . service . jca . CachedConnectionValve . invoke ( CachedConnectionValve . java : 158 )
at org . apache . catalina . core . StandardEngineValve . invoke ( StandardEngineValve . java : 109 )
at org . apache . catalina . connector . CoyoteAdapter . service ( CoyoteAdapter . java : 330 )
at org . apache . coyote . http11 . Http11Processor . process ( Http11Processor . java : 829 )
at org . apache . coyote . http11 . Http11Protocol $ Http11ConnectionHandler . process ( Http11Protocol . java : 598 )
at org . apache . tomcat . util . net . JIoEndpoint $ Worker . run ( JIoEndpoint . java : 447 )
at java . lang . Thread . run ( Thread . java : 662 )
abaixo o código do meu Interceptor
package br.com.meuprojeto.web.interceptors ;
import javax.servlet.http.HttpServletRequest ;
import org.hibernate.StaleObjectStateException ;
import org.springframework.beans.factory.annotation.Autowired ;
import br.com.caelum.vraptor.InterceptionException ;
import br.com.caelum.vraptor.Intercepts ;
import br.com.caelum.vraptor.Result ;
import br.com.caelum.vraptor.core.InterceptorStack ;
import br.com.caelum.vraptor.interceptor.Interceptor ;
import br.com.caelum.vraptor.ioc.RequestScoped ;
import br.com.caelum.vraptor.resource.ResourceMethod ;
import br.com.caelum.vraptor.util.hibernate.HibernateTransactionInterceptor ;
@Intercepts ( after = HibernateTransactionInterceptor . class )
@RequestScoped
public class StaleObjectStateExceptionInterceptor implements Interceptor {
private Result result ;
private HttpServletRequest request ;
@Autowired
public StaleObjectStateExceptionInterceptor ( Result result , HttpServletRequest request ) {
this . result = result ;
this . request = request ;
}
@Override
public boolean accepts ( final ResourceMethod method ) {
if ( method . getMethod (). getName (). equals ( "saveOrUpdate" )) {
return true ;
}
return false ;
}
@Override
public void intercept ( InterceptorStack stack , ResourceMethod method , Object object ) throws InterceptionException {
try {
stack . next ( method , object );
} catch ( StaleObjectStateException e ) {
System . out . println ( "Testando StaleObjectStateException..." );
} catch ( Exception e ) {
System . out . println ( "Testando Exception..." );
}
}
}
e outra dúvida… o HibernateTransactionInterceptor é um open session in view?
obrigado
Lucas_Cavalcanti 13 de jun. de 2011
desculpe, vc está usando jpa… o interceptor é JPATransactionInterceptor. Use:
@Intercepts ( after = JPATransactionInterceptor . class )
e sim, ele é um OpenSessionInView.
Detalhe importante: vc está controlando transações ou é o VRaptor que está fazendo isso? se for você isso que eu falei não vai funcionar
CommanderShepard 13 de jun. de 2011
quem está controlando as transações é o Spring
será que tem jeito de pegar o StaleObjectStateException via Interceptor?
ficaria perfeito pra mim pois já tenho um Interceptor para erros genéricos que faz isso (o que recebe o objeto “Exception exception”)
aí seria apenas adicionar mais um catch no try
Lucas_Cavalcanti 13 de jun. de 2011
vc está usando o @Transactional então, certo?
o problema é que ele tá escondendo a exception, não sei se vc vai conseguir capturá-la de um jeito legal…
pq vc quer fazer isso mesmo? vc vai conseguir se recuperar do erro? ou é só logging?
CommanderShepard 13 de jun. de 2011
isto
estou utilizando "@Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)"
eu vou me recuperar do StaleObjectStateException e mostrar uma mensagem pro usuário: "Este registro foi atualizado por outro usuário, recarregue os dados e tente novamente."
essa vai ser a mensagem padrão pra quando algum erro de concorrência acontecer
e eu tb não to conseguindo pegar o StaleObjectStateException direto do meu Controller
// No meu Controller , no método de salvar ...
try {
super . saveOrUpdate ( objeto );
} catch ( Exception e ) { // Esperando um StaleObjectStateException aqui .
e . printStackTrace (); // Apenas o TransactionSystemException e o RollbackException .
}
tah foda
Lucas_Cavalcanti 13 de jun. de 2011
tenta fazer esse try…catch mais perto da session/entityManager… por exemplo no dao ou coisa do tipo