ho una web application che gira su weblogic e si collega a oracle; non l' ho fatta io (almeno nessuno mi sgrida :-)) ma ho guardato la parte di connessione e fa qualcosa del genere
codice:public class DBUtility { private static Connection connection = null; public static Connection getConnection() { if (connection != null) return connection; else { try { String driver ="oracle.jdbc.driver.OracleDriver"; String url ="jdbc:oracle:thin:@DBSCA04:1889:SCA04"; String user ="user"; String password ="password"; Class.forName(driver); connection = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return connection; // } } } } public class DBCrudCountry { private Connection connection; .... public DBCrudCountry() { connection = DBUtility.getConnection(); } public void updateItem(Country item, int delete) { delete(item); insert(item, delete); } private void insert(Country item, int delete) { ... } private void delete(Country item) { PreparedStatement preparedStatement; try { preparedStatement = connection.prepareStatement("update " + tableName + " set DELETED=?" + " where ID=?"); preparedStatement.setInt(1, 1); preparedStatement.setInt(2, item.getId()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } public class DBControllerCountry extends HttpServlet { private DBCrudCountry crud; public DBControllerCountry() { crud = new DBCrudCountry(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("action") != null) { if (action.equals("list")) { ... } else if ( action.equals("delete")) { ... try { if (action.equals("create")) { .. } else if (action.equals("update")) { ... } else if (action.equals("delete")) { if (request.getParameter("id") != null) { item = crud.getItem(Integer.parseInt(request.getParameter("id"))); crud.updateItem(item, 1); } } } catch (Exception ex) { String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getStackTrace().toString() + "}"; response.getWriter().print(error); } } } } }
funziona perfettamente ma ogni giorno devo riavviare nel senso che se oggi tutto funziona, domani mattina arrivo qua e la connessione fallisce; nei log mi da questi errori
java.sql.SQLRecoverableException: IO Error: Broken pipe
...
Caused by: java.net.SocketException: Broken pipe
...
java.sql.SQLRecoverableException: Closed Connection
dal supporto capisco che
i database vengono backuppati a freddo tutte le notti. Pertanto le connessioni lasciate attive senza un sistema di auto-reconnect non risultano più attive.
posso rimediare al codice che ho postato senza stravolgere tutto?
a me non pare correttissimo ma ho bisogno di fare nella maniera più veloce possibile, almeno per ora
grazie

Rispondi quotando