我是一名新手,负责使用HikariPool连接建立宿舍管理应用程序。现在,我有时会收到此错误java.sql.SQLTransientConnectionException: HikariPool-1- connection is not available
,一旦发生错误,就会弄乱我的应用程序数据。例如,在我的下面的代码中,我在paymentRecord()
中调用函数AssignRoom()
以在分配房间后执行。
我在每个函数中打开和关闭连接(AssignRoom()和paymentRecord())但有时,其中一个函数无法执行。它是AssignRoom()正确执行而paymentRecord()无法执行,反之亦然。
我的代码可能非常糟糕,但我是新手试图学习。
PS:现在我在测试具有良好互联网连接的应用程序时从未遇到过这个问题,但是我的客户确实报告了此问题,而且他主要有互联网连接问题。
我有时认为互联网连接可能是我问题的一个因素,如果是这样,我为什么要处理这个?
分配房间
public void AssignRoom(){
try
{
java.sql.Date sqldate = new java.sql.Date(date.getDate().getTime());
conn = DBConnection.getConn();
String query = "Insert into AssignRoom(Floor,RoomNumber,Student,DateAssigned) values (?,?,?,?)";
PreparedStatement pst = conn.prepareStatement(query);
pst.setString(1, Floor.getSelectedItem().toString());
pst.setString(2, FindRoom.getSelectedItem().toString());
pst.setString(3, FindStd.getSelectedItem().toString());
pst.setDate(4, sqldate);
pst.execute();
if(seats.getText().equals("0")){
JOptionPane.showMessageDialog(null, "Room is fully occupied");
}
else
{
JOptionPane.showMessageDialog(null, "Room Successfully Assigned");
UpdateAvailableBeds();
paymentRecord();
conn.close();
JOptionPane.showMessageDialog(null, "Please wait...Processing payment");
navToPayment();
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}
付款记录
public void paymentRecord()
{
try
{
java.sql.Date sqldate = new java.sql.Date(date.getDate().getTime());
System.out.println("Cheking "+sqldate);
System.out.println("Cheking "+FindRoom.getSelectedItem().toString());
System.out.println("Cheking "+FindStd.getSelectedItem().toString());
conn = DBConnection.getConn();
String query = "Insert into PaymentRecords(Student_Name,Room_Number,DateAssigned,Total_Amount,Paid,Balance) values(?,?,?,?,?,?)";
String sql = "select Price from Rooms where RoomNumber =" +FindRoom.getSelectedItem()+ " and Floor ='"+Floor.getSelectedItem().toString()+"'";
PreparedStatement pst = conn.prepareStatement(query);
PreparedStatement pst1 = conn.prepareStatement(sql);
// ResultSet rs = pst.executeUpdate(query);
ResultSet rs1 = pst1.executeQuery(sql);
while(rs1.next())
{
FullAmount = rs1.getString("Price");
Price = Float.parseFloat(FullAmount);
Rem = Float.parseFloat(AmtPaid.getText());
Balance = Price - Rem;
System.out.println("Cheking "+FullAmount);
}
pst.setString(1, FindStd.getSelectedItem().toString());
pst.setString(2, FindRoom.getSelectedItem().toString());
pst.setDate(3, sqldate);
pst.setString(4, FullAmount);
pst.setString(5, AmtPaid.getText());
pst.setFloat(6, Balance);
pst.execute();
pst1.execute();
conn.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}