Newsgroup: it.comp.java
Titolo: Ejb3 interazione con DB

Salve a tutti,
sto affrontando il mio primo progetto EJB3, e sto incontrando non
poche difficoltà. Uno dei problemi è il seguente, mi trovo a dover
effettuare un inserimento di un nuovo oggetto in un DB. Nei progetti
Web precedenti (con sole jsp e servlet), avrei direttamente lanciato
la query sql dalla servlet. In questo modo sarei riuscito a gestire
facilmente il risultato della INSERT:
-Inserimento non eseguito (ad esempio a causa di chiave dubplicata),
ricevevo un'eccezione, che catturata da una catch mi permette di
inviare alla jsp un mex che avverete l'utente del problema;
-Inserimento andato a buon fine invio all'utente un messaggio di
operazione eseguita.
In questo caso invece non riesco, infatti nel momento in cui vado ad
eseguire l'inserimento, se la chiave è duplicata l'applicazione va in
crash e non ritorna nulla.
C'è qualche modo per risolvere questo problema, evitando di dover
eseguire prima una select di verifica sull'esistenza del record, e
quindi di dover interagire due volte con il database.
Grazie a tutti per l'aiuto
Buona Giornata

 crash e non ritorna nulla.
penso che tu sia un po' in confusione.
una applicazione non va ineluttabilmente in crash perché sul database
c'è una chiave duplicata ma semmai perché tu non gestisci come si deve
l'eccezione che ne deriva, quindi sarebbe opportuno tu spiegassi cosa
intendi per "andare in crash". inoltre va detto che gli ejb sono
transazionali ed è essenziale gestire come si deve le eccezioni per
evitare commit o rollback a casaccio.
mi spiace ma per come poni la domanda è praticamente impossibile
aiutarti.

Mi dispiace per non essere stato chiaro. La mia domanda è proprio
questa, come fare per gestire l'eccezione. Mentre nella servlet
gestisco questa con una try catch (SqlException), qui non so come
fare.
Per andare in crash intendo che l'applicazione si blocca, se vado a
vedere l'ouput del Jboss da un errore ed il problema è proprio la
chiave duplicata.
Io sto utilizzando il metodo:
Entity manager em;
em.persist(nuovo_record);
Forse sono io che sto sbagliando metodo? oppure devo usare qualche try
catch che cattura qualche eccezione particolare?
Vorrei appunto un consiglio su questo, quale è il codice corretto per
seguire operazioni di questo tipo in perfetta sicurezza e riuscendo a
gestire tutte le eccezioni del caso.
Grazie ancora per l'aiuto
ALessandro

 fare.
non viene sollevata una sqlexception:
http://download.oracle.com/javaee/5/api/javax/persistence/EntityManager.html#persist(java.lang.Object)
il primo problema, quindi, è che l'eccezione non la catturi.
in generale comunque è corretto che tu gestisca le eccezioni a livello 
di view.

Ricapitolando, correggimi se sbaglio:
Il metodo del session bean lo metto come Throw exception:
public void insert() throw Exception
{
...
manager.persist(manutentore);
...
  }
Dopo di che gestisco l'eccezione dalla servlet che lo chiama con  una
try catch, la quale manderà un messaggio piuttosto che un'altro alla
jsp che visualizza l'utente.
E' corretto?

Non entra mai nel catch(Exception) nonostante vada in errore. Ti
incollo la lista degli errori che vengono mostrati da JBOSS, non so se
possono essere utili.
Ciao
Alessandro
12:57:17,849 ERROR [JDBCExceptionReporter] Duplicate entry 'loreeeeee'
for key 'PRIMARY'
12:57:17,850 ERROR [AbstractFlushingEventListener] Could not
synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
	at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
94)
	at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
66)
	at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
275)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
266)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
167)
	at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:
321)
	at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:
50)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
	at org.hibernate.ejb.AbstractEntityManagerImpl
$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
	at
com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:
101)
	at
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:
263)
	at
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:
86)
	at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
	at
com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:
1414)
	at
com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:
135)
	at
com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:
75)
	at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
	at org.jboss.aspects.tx.TxInterceptor
$Required.invoke(TxInterceptor.java:190)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:
76)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:
186)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:
41)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:
106)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:
67)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:
67)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:
219)
	at
org.jboss.ejb3.proxy.handler.ProxyInvocationHandlerBase.invoke(ProxyInvocationHandlerBase.java:
261)
	at
org.jboss.ejb3.proxy.handler.session.SessionSpecProxyInvocationHandlerBase.invoke(SessionSpecProxyInvocationHandlerBase.java:
101)
	at $Proxy118.insert(Unknown Source)
	at TestServlet.doGet(TestServlet.java:27)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)
	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:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:
447)
	at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: Duplicate entry 'loreeeeee'
for key 'PRIMARY'
	at
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:
2020)
	at
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:
1451)
	at
org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:
774)
	at
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:
70)
	at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
268)
	... 59 more
12:57:17,855 WARN  [arjLoggerI18N]
[com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2]
TwoPhaseCoordinator.beforeCompletion - failed for
com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@1917d6d
javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
	at
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:
614)
	at org.hibernate.ejb.AbstractEntityManagerImpl
$1.beforeCompletion(AbstractEntityManagerImpl.java:513)
	at
com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:
101)
	at
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:
263)
	at
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:
86)
	at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
	at
com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:
1414)
	at
com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:
135)
	at
com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:
75)
	at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
	at org.jboss.aspects.tx.TxInterceptor
$Required.invoke(TxInterceptor.java:190)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:
76)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:
186)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:
41)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:
106)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:
67)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:
67)
	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
	at
org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:
219)
	at
org.jboss.ejb3.proxy.handler.ProxyInvocationHandlerBase.invoke(ProxyInvocationHandlerBase.java:
261)
	at
org.jboss.ejb3.proxy.handler.session.SessionSpecProxyInvocationHandlerBase.invoke(SessionSpecProxyInvocationHandlerBase.java:
101)
	at $Proxy118.insert(Unknown Source)
	at TestServlet.doGet(TestServlet.java:27)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)
	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:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:
447)
	at java.lang.Thread.run(Thread.java:595)
Caused by: org.hibernate.exception.ConstraintViolationException: Could
not execute JDBC batch update
	at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
94)
	at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
66)
	at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
275)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
266)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
167)
	at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:
321)
	at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:
50)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
	at org.hibernate.ejb.AbstractEntityManagerImpl
$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
	... 52 more
Caused by: java.sql.BatchUpdateException: Duplicate entry 'loreeeeee'
for key 'PRIMARY'
	at
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:
2020)
	at
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:
1451)
	at
org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:
774)
	at
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:
70)
	at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
268)
	... 59 more
12:57:17,857 ERROR [STDERR]
javax.ejb.EJBTransactionRolledbackException: Transaction rolled back
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.tx.Ejb3TxPolicy.handleEndTransactionException(Ejb3TxPolicy.java:
54)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
12:57:17,858 ERROR [STDERR] 	at org.jboss.aspects.tx.TxInterceptor
$Required.invoke(TxInterceptor.java:190)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:
76)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:
186)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:
41)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:
106)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:
67)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:
67)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
102)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:
219)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.proxy.handler.ProxyInvocationHandlerBase.invoke(ProxyInvocationHandlerBase.java:
261)
12:57:17,858 ERROR [STDERR] 	at
org.jboss.ejb3.proxy.handler.session.SessionSpecProxyInvocationHandlerBase.invoke(SessionSpecProxyInvocationHandlerBase.java:
101)
12:57:17,858 ERROR [STDERR] 	at $Proxy118.insert(Unknown Source)
12:57:17,859 ERROR [STDERR] 	at TestServlet.doGet(TestServlet.java:27)
12:57:17,859 ERROR [STDERR] 	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
12:57:17,859 ERROR [STDERR] 	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
206)
12:57:17,859 ERROR [STDERR] 	at
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:
96)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
235)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
206)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
235)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:
191)
12:57:17,859 ERROR [STDERR] 	at
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:
190)
12:57:17,859 ERROR [STDERR] 	at
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:
92)
12:57:17,859 ERROR [STDERR] 	at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:
126)
12:57:17,859 ERROR [STDERR] 	at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:
70)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:
127)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
102)
12:57:17,859 ERROR [STDERR] 	at
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:
158)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
109)
12:57:17,859 ERROR [STDERR] 	at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
330)
12:57:17,859 ERROR [STDERR] 	at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:
829)
12:57:17,859 ERROR [STDERR] 	at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:601)
12:57:17,859 ERROR [STDERR] 	at org.apache.tomcat.util.net.JIoEndpoint
$Worker.run(JIoEndpoint.java:447)
12:57:17,859 ERROR [STDERR] 	at java.lang.Thread.run(Thread.java:595)
12:57:17,860 ERROR [STDERR] Caused by:
javax.transaction.RollbackException:
[com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted]
[com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted]
Could not commit transaction.
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:
1426)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:
135)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:
75)
12:57:17,860 ERROR [STDERR] 	at
org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
12:57:17,860 ERROR [STDERR] 	... 44 more
12:57:17,860 ERROR [STDERR] Caused by:
javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:
614)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.ejb.AbstractEntityManagerImpl
$1.beforeCompletion(AbstractEntityManagerImpl.java:513)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:
101)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:
263)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:
86)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
12:57:17,860 ERROR [STDERR] 	at
com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:
1414)
12:57:17,860 ERROR [STDERR] 	... 47 more
12:57:17,860 ERROR [STDERR] Caused by:
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
94)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
66)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
275)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:
321)
12:57:17,860 ERROR [STDERR] 	at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:
50)
12:57:17,861 ERROR [STDERR] 	at
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
12:57:17,861 ERROR [STDERR] 	at
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
12:57:17,861 ERROR [STDERR] 	at
org.hibernate.ejb.AbstractEntityManagerImpl
$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
12:57:17,861 ERROR [STDERR] 	... 52 more
12:57:17,861 ERROR [STDERR] Caused by: java.sql.BatchUpdateException:
Duplicate entry 'loreeeeee' for key 'PRIMARY'
12:57:17,861 ERROR [STDERR] 	at
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:
2020)
12:57:17,861 ERROR [STDERR] 	at
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:
1451)
12:57:17,861 ERROR [STDERR] 	at
org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:
774)
12:57:17,861 ERROR [STDERR] 	at
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:
70)
12:57:17,861 ERROR [STDERR] 	at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
268)
12:57:17,861 ERROR [STDERR] 	... 59 more
12:57:17,861 ERROR [[TestServlet]] Servlet.service() for servlet
TestServlet threw exception
java.lang.NullPointerException
	at TestServlet.doGet(TestServlet.java:36)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)
	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:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:
447)
	at java.lang.Thread.run(Thread.java:595)

 execute JDBC batch update
Cosě a naso mi sembra che tu stia usando la strategia "Open Session in 
View":
http://community.jboss.org/wiki/OpenSessionInView
ovvero apri la transazione appena il server comincia a processare la 
richiesta, per poi eseguire il flush alla fine. Flush che fallisce in 
caso di errori, ma ormai č troppo tardi per catturarli!
Prova a fare una chiamata ad EntityManager.flush() appena dopo le varie 
operazioni di persistenza, l'errore dovrebbe accadere all'interno del 
tuo codice e l'eccezione catturata :)
Facce sape'
-- 
Dimitri De Franciscis
Phone:  
(+39) 3401570778
Website: 
http://www.megadix.it/

Ciao,
Allora in questo modo, effettivamente riesco a catturare l'errore, nel
senso che l'applicazione java entra nel catch e posso ritornare alla
mia servlet senza che l'applicazioni si pianti.
Il mio dubbio però è questo, il Jboss comunque mi spara in output la
stessa lista di errori che ho postato precedentemente e non credo che
sia cosa buona e giusta :P. C'è qualche modo per evitare questo o no?
Io non sono esperto quindi magari tu puoi aiutarmi, in questi casi in
inserimento di un nuovo record quale è l'approccio più comune?Si fa
prima una select per verificare ed in caso si fa la insert, o si cerca
di catturare l'errore come sto procedendo io?
Grazie del supporto
Alessandro