| |

VerySource

 Forgot password?
 Register
Search
View: 871|Reply: 5

Database shutdown question

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-15 22:30:02
| Show all posts |Read mode
Connection con = DriverManager.getConnection (url, "123", "123");
       Statement stmt = con.createStatement ();
       ResultSet rs = stmt.executeQuery (sql);
       Suppose there is a long code (20 lines)
       Suppose that the code in these 20 lines reports a SQLException or other end
       rs.close
       stmt.close
       con.close
       The above three sentences will not be executed, and the connection cannot be closed
      

       How can I close all connections to the database and the method still throws an exception upwards?
       This method throws SQLException
       Try catch words! too big! !!
Reply

Use magic Report

1

Threads

19

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-4-16 18:45:01
| Show all posts
Closing timing can be wrong
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-17 09:00:02
| Show all posts
That's wrong! There must be closed
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-4-18 12:45:01
| Show all posts
try {
      tx = session.beginTransaction ();
          } catch (Exception ex) {
      throw SQLException;}
    finally {
         rs.close;
       stmt.close;
       con.close;

    }
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-4-18 16:15:01
| Show all posts
try {
// Make DB Connection
// Do whatever you need
}
catch (SQLException e) {
// Do Exception handling
// throw e; // if you have to.
}
finally {
  if (DBConnection is still open)
    Close it.
  // Release all necessary resources.
}
Reply

Use magic Report

0

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Invalid IP Address

Post time: 2020-5-18 16:30:02
| Show all posts
Upstairs positive solution, get a finally, he doesn't want to close
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list