java多线程访问数据库存在延迟十五分钟,如何解决??

时间:2024-11-24 09:15:19
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class DatabaseAccess { private static final DataSource dataSource = createDataSource(); private static DataSource createDataSource() { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/your_database"); config.setUsername("your_username"); config.setPassword("your_password"); config.setMaximumPoolSize(10); // 根据需要调整连接池大小 config.setConnectionTimeout(30000); // 连接超时时间 return new HikariDataSource(config); } public static void main(String[] args) { try (Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM your_table"); ResultSet rs = stmt.executeQuery()) { while (rs.next()) { // 处理结果集 } } catch (Exception e) { e.printStackTrace(); } } }