Message Boards

Check connection external DDBB

Iñigo Boyano, modified 3 Years ago.

Check connection external DDBB

Junior Member Posts: 96 Join Date: 2/4/14 Recent Posts
Hi, 
I have a external connection  in my application using service Builder.The connection works properly, i create the new dataSource, setup the correct properties jdbc.* and the app works well.My question is related to how to check before the portlet is executed, that this connection is up. I've thought to create a method in the activate method of OSGI, to verify the connection is up before deploy the portlet or maybe create this method in the Porlet class before  load the RenderCommand.

The requirement is to show a error message if the connection is not ready, before load the portlet.
What is the best way to achieve this?
Kind regards, 
Iñigo
thumbnail
Olaf Kock, modified 3 Years ago.

RE: Check connection external DDBB

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
Iñigo Boyano:

The requirement is to show a error message if the connection is not ready, before load the portlet.

If your database goes away so routinely that you're explicitly planning for this, you could just access (read) some data from it when the portlet is rendered. However, you'll need to be aware that your database (as fragile as it seems to be) disappears between displaying a portlet and submitting data. So you'll need proper error handling upon submission first.

I'd opt for stabilizing the underlying database, so that it's an absolute exception for it to be unavailable (not knowing the exact circumstances that you deal with)
Iñigo Boyano, modified 3 Years ago.

RE: Check connection external DDBB

Junior Member Posts: 96 Join Date: 2/4/14 Recent Posts
Thanks Olaf for your answer.

The idea is what you propose.
First of all, check if i can connect to my external database, and if not, show an error page with a portlet unavailable message.My circustances are that this database is only accesible in a closed network, or i am in that network or I have to connect to it by VPN.

So, instead to show the classic liferay red message error when the portlet is unavailable, i want to catch this error/exception in my init() method of the main prtlet for example in order to show a custom page with a custom message.

I don´t know if develop a classic connection like
"connection = DriverManager.getConnection(dburl, PropsUtil.get(SearchEcdPortletKeys.EXTERNAL_DDBB_USERNAME_PROP),PropsUtil.get(SearchEcdPortletKeys.EXTERNAL_DDBB_PASSWORD_PROP));"

 is a bad practice or what is the way to do this whit service builder.

Kind regards, 

Iñigo
thumbnail
Olaf Kock, modified 3 Years ago.

RE: Check connection external DDBB

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
You could just obtain the connection, as you said (I haven't checked the code, but it should be easy to check if it works or not, and under which circumstances).
However, placing this code in init() is plain wrong: init() is typically called exactly once, and most likely right after server start (or rather right after the portlet's deployment) - just like a servlet's init() would be. You'd have to implement this in a portlet's render phase, and if you try to read anything from your database but get an exception that hints at the database's unavailability: Catch that exception and show an alternative message. Liferay only shows the "portlet unavailable" when the render phase triggers any uncaught exception. If you catch it yourself, it's up to you to determine what you show.
I'd just access the database through servicebuilder, without mocking around with the underlying connection, and catch a resulting exception, rather than coding low level database stuff. You could even hide this in your LocalServiceImpl and implement an available() (or whatever smarter name you come up with) method there.