You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by jamest <ji...@gmail.com> on 2017/03/09 18:42:58 UTC

camel-jpa and PersistenceException

I am using camel-jpa to poll messages from a database.  As recommended for
recovery on database failures, the persistence unit is configured with
Tomcat jdbc connection pool as the datasource and uses a test-on-borrow
strategy to validate connections.

The route still fails to recover if the database goes down.  I was able to
recreate the problem using an HSQL instance that I forced to go down during
route execution.  The stack trace shows that the PersistenceException is
thrown from JpaConsumer's doInTransaction().

As I was researching the problem I came across CAMEL-9608 which introduced
catching a PersistenceException and forcing the next poll to create a new
EntityManager.  It only performs this when the PersistenceException is
thrown from processBatch() 

So my question is, if PersistenceException can be thrown at
TransactionTemplate.execute() then is there any reason why the try/catch for
PersistenceException can't wrap this too?

From reading this forum I know that the JPA component expects the
EntityManager to handle the connection recovery but I'm not on solid ground
yet in determining how the EntityManager is supposed to react under these
circumstances.  It seems like the locally stored EntityManager just doesn't
give up this bad connection.






--
View this message in context: http://camel.465427.n5.nabble.com/camel-jpa-and-PersistenceException-tp5795205.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-jpa and PersistenceException

Posted by jamest <ji...@gmail.com>.
Anyone have feedback on the patched version?  

Manual testing with the patched version in our product was successful,
basically just starting/stopping the database and observing that the
component was always able to reconnect.  Without this change it continues to
throw the exception first reported.

I haven't worked out a good unit test strategy for this but I have a couple
of ideas using an embedded hsqldb.  





--
View this message in context: http://camel.465427.n5.nabble.com/camel-jpa-and-PersistenceException-tp5795205p5795841.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-jpa and PersistenceException

Posted by jamest <ji...@gmail.com>.
It doesn't look very clean perhaps but adding the try/catch around the whole
transactionTemplate.execute() block worked for us.

   
https://github.com/jamesET/camel/blob/master/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java

As the stack trace confirms, persistence exceptions can be thrown just from
the back-end mechanics that start the transaction and could potentially
happen at other points in the poll method, eg query.getResultList().  And if
the  the entity manager should be refreshed in case of rollback, or even for
a hard disconnect in my case, then it seems reasonable to do something like
this.



--
View this message in context: http://camel.465427.n5.nabble.com/camel-jpa-and-PersistenceException-tp5795205p5795550.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-jpa and PersistenceException

Posted by jamest <ji...@gmail.com>.
I opened a JIRA an attached a patch  CAMEL-11197
<https://issues.apache.org/jira/browse/CAMEL-11197>  

I couldn't come up with a way to unit test this change without introducing
integration tests with various connection pool providers but I am using this
patch in my project already.  No more problems with lost database connection



--
View this message in context: http://camel.465427.n5.nabble.com/camel-jpa-and-PersistenceException-tp5795205p5798255.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-jpa and PersistenceException

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You could try to patch the code yourself and run with a camel-jpa JAR
with your code changes to see what you can get working for you, and
then come back here with the code changes for further discussion and
see what we can do for camel-jpa.

On Thu, Mar 9, 2017 at 7:42 PM, jamest <ji...@gmail.com> wrote:
> I am using camel-jpa to poll messages from a database.  As recommended for
> recovery on database failures, the persistence unit is configured with
> Tomcat jdbc connection pool as the datasource and uses a test-on-borrow
> strategy to validate connections.
>
> The route still fails to recover if the database goes down.  I was able to
> recreate the problem using an HSQL instance that I forced to go down during
> route execution.  The stack trace shows that the PersistenceException is
> thrown from JpaConsumer's doInTransaction().
>
> As I was researching the problem I came across CAMEL-9608 which introduced
> catching a PersistenceException and forcing the next poll to create a new
> EntityManager.  It only performs this when the PersistenceException is
> thrown from processBatch()
>
> So my question is, if PersistenceException can be thrown at
> TransactionTemplate.execute() then is there any reason why the try/catch for
> PersistenceException can't wrap this too?
>
> From reading this forum I know that the JPA component expects the
> EntityManager to handle the connection recovery but I'm not on solid ground
> yet in determining how the EntityManager is supposed to react under these
> circumstances.  It seems like the locally stored EntityManager just doesn't
> give up this bad connection.
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-jpa-and-PersistenceException-tp5795205.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2