0

Edit

I have created Task which contains method which loads the content. I need task, because I want to set progress bar into undetermined state during data loading. When data is loaded, progress bar is set to "1" (completed).

What i want to achieve is: user logs in, but there is no connection so progress bar indicates that application is trying to connect and after a while and error message will pop-up and do this whenever connection is lost user tries to do something. But again, nothing is happening, this time no exception thrown.

It all freezes at conn = DriverManager.getConnection(DB_URL, USER, PASS), isValid() is not helping. End of Edit

Short version:

When connection with DB is lost, application hangs up and no exception is thrown, so i have nothing to catch and inform user about problem

Long version:

I am trying to simulate a "connection lost" situation in my application. What I want to achieve is to display "Connection to database has been lost" Dialog windows to infrom user about problem. This exception is called SQLNonTransientConnectionException. My problem is, I only receive it during login process. I have login windows, and if there is no connection, an exception is thrown.

But what if user lost connection after login? Like he is successfully logged in and during inserting/updating some table the connection is lost? In this case application just hangs up, it is not responding and no exception is thrown, so i have nothing to catch and infrom user about problem.

Could please help me how to achieve this?

Mr. Crow
  • 27
  • 12
  • You could start with [`Connection.isValid`](https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#isValid%28int%29) but I believe there is a parameter to add in the URI that allows to fail if the connection is closed. – AxelH May 11 '18 at 11:59
  • Have you tried [setting a timeout?](https://docs.oracle.com/javase/9/docs/api/java/sql/Statement.html#setQueryTimeout-int-) – fabian May 11 '18 at 12:42
  • Thank you guys for tips, will try both – Mr. Crow May 11 '18 at 13:32

2 Answers2

1

Here people recommend using connection pooling library instead of implementing it yourself: What the standard way in jdbc to manage lost connection?. This should handle connection lost better. (No experience with it myself)

To summarize the link, it recommends to use the The DBCP Component

Rob
  • 26,989
  • 16
  • 82
  • 98
findusl
  • 2,454
  • 8
  • 32
  • 51
0

For some reason when i put catch (SQLNonTansientConnectionException clause into try statement... instead of catch (SQLException) it works.

Mr. Crow
  • 27
  • 12
  • That makes no sense at all, as `SQLNonTansientConnectionException` is a `SQLException`, so the catch clause using `SQLException` should catch `SQLNonTansientConnectionException` as well. – Mark Rotteveel May 12 '18 at 07:35
  • okey, l forgot to mantion that i inserted catch (SQLNonTansientConnectionException clause in front of catch (SQLException). However now i have another problem – Mr. Crow May 15 '18 at 11:27