現(xiàn)有一耗時操作,為避免請求長時間未返回而拋出異常設置了超時時間
HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mysql");
config.setUsername("root");
config.setPassword("123456");
config.setConnectionTimeout(1000); // 設置超時時間1秒
為了明確setConnectionTimeout()的作用,我導入了30萬條數據進行查詢并將超時時間設置為1秒,啟動后查詢出結果并耗時2秒
【預期結果】由于設置了timeout為1秒,實際耗時2秒,所以會拋出超時異常
【實際結果】沒有拋出超時異常,耗時2秒查詢出了結果
請教前輩們我是不是對JDBC中的timeout有誤解呢?這里的timeout不是指查詢超時時間?
1 回答

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
不是。這個是獲取連接的超時時間,就是從連接池返回連接的超時時間。
connectionTimeout
This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
上面的是官方描述。
SQL 執(zhí)行超時時間JDBC
可以直接使用 Statement.setQueryTimeoutSpring
可以使用 @Transactional(timeout=10)
添加回答
舉報
0/150
提交
取消