亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

可以在多個類方法中使用相同的 BasicDataSource、Connection

可以在多個類方法中使用相同的 BasicDataSource、Connection

臨摹微笑 2022-07-20 19:27:28
下面的代碼使用了 BasicDataSource、Sql Connection、Statement 和 ResultSet 的靜態對象。下面的代碼運行良好,但我只想知道使用這些編碼實踐的安全性?;蛘呶以撊绾蝺灮旅娴拇a,使其變得更加穩定和可靠。public class Testing {     static BasicDataSource bds = DBConnection.getInstance().getBds();     static Connection con = null;     static PreparedStatement stmt = null;     static ResultSet rs = null;    private void show() {        try {            con = bds.getConnection();            stmt = con.prepareStatement("SELECT * FROM users");            rs = stmt.executeQuery();            if(rs.next()) {                System.out.println(rs.getString("firstname") + " " + rs.getString("lastname"));            }        } catch (SQLException e) {            e.printStackTrace();        }finally {            try {                con.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    private void display() {        try {            con = bds.getConnection();            stmt = con.prepareStatement("SELECT * FROM agent_cities");            rs = stmt.executeQuery();            while(rs.next()) {                System.out.println(rs.getString("city_name"));            }提前致謝。請分享您可以打破上述代碼并質疑其安全性的案例。更新: 更新只是為了確保沒有人應該像我在上面的程序中使用的那樣使用靜態字段,因為上面的程序在部署到開發服務器上時包含錯誤。在大型系統上使用上述代碼后,我發現了這個錯誤。一個月前我對上面的代碼沒有任何問題,它工作正常,但今天我發現了這個錯誤。漏洞:在點擊我的 API 6-7 次后,它在第 8 次點擊時停止響應。我真的不知道為什么,也不知道程序中存在循環漏洞。但是現在我已經接受了答案,我更改了我的源代碼并開始在我的代碼中使用 try-with 資源并刪除了靜態字段。但是我仍然很想知道我在上面的代碼中發現的錯誤。在 7-8 API 命中后沒有響應并掛起。請分享您對此的看法。我正在使用 apache tomcat 8.5.32 服務器。提前致謝。
查看完整描述

3 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

更好地使用try-with-resources。這會自動關閉 Connection、Statement 和 ResultSet,即使在引發異常或內部返回時也是如此。


    String sql = "UPDATE users SET firstname = ? WHERE id = ?";

    try (Connection con = bds.getConnection();

            PreparedStatement stmt = con.prepareStatement()) {

        stmt.setString(1, "shsh");

        stmt.setLong(2, 2);

        stmt.executeUpdate();

        System.out.println("updated successfully");

    }


    String sql = "SELECT city_name FROM agent_cities";

    try (Connection con = bds.getConnection();

            PreparedStatement stmt = con.prepareStatement()) {

        try (ResultSet rs = stmt.executeQuery()) {

            while(rs.next()) {

                System.out.println(rs.getString("city_name"));

            }

        }

    }

這對于垃圾收集更好。防止不美觀的 rs2、rs3。允許多用戶并發,例如在服務器應用程序中。調用自己的查詢。并且static更多的是全局變量的風格。


查看完整回答
反對 回復 2022-07-20
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

如果我們談論這樣一個小程序,它或多或少是可以的。但是沒有必要將 con、stmt 和 rs 作為靜態變量保留,它們可以在方法內部聲明。此外,您需要重寫 try catch finally 塊并正確關閉資源:


Connection con = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

  // your code

} catch (SQLException e) {

  e.printStackTrace();

} finally {

  try { if (rs != null) rs.close(); } catch (Exception e) {e.printStackTrace();}

  try { if (stmt != null) stmt.close(); } catch (Exception e) {e.printStackTrace();}

  try { if (conn != null) conn.close(); } catch (Exception e) {e.printStackTrace();}

}

作為下一步,您可以檢查try-with-resources構造以清理此代碼。


查看完整回答
反對 回復 2022-07-20
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您應該使用 try-with-resources 來避免任何類型的連接泄漏。


以下示例從文件中讀取第一行。它使用 BufferedReader 的實例從文件中讀取數據。BufferedReader 是程序完成后必須關閉的資源:


static String readFirstLineFromFile(String path) throws IOException {

    try (BufferedReader br =

                   new BufferedReader(new FileReader(path))) {

        return br.readLine();

    }

}


查看完整回答
反對 回復 2022-07-20
  • 3 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號