gpt4 book ai didi

java - 服务器中的线程卡住

转载 作者:行者123 更新时间:2023-12-02 09:25:50 24 4
gpt4 key购买 nike

我的java类是这样的

package com.indiabulls.portfoliotracker.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.indiabulls.trade.dbConnection.JDBCConnection;


public class AppKeyValidationDao {
private static JDBCConnection instance;
private static Connection con;
private static PreparedStatement pstmntAppKeyValidate = null;
private static long lastAccess;
private static Statement stmt = null;

private static String strappKeyValidate = "select count(1) from tablename";

static {
try {
instance = new JDBCConnection();
con = instance.getConnection();
pstmntAppKeyValidate = con.prepareStatement(strappKeyValidate);
lastAccess = System.currentTimeMillis();
} catch (SQLException e) {
}
}

/**
*
* @param userId
* @param AppKey
* @return this method will used for validate app Key from the database
* @throws SQLException
*/
public static String validateAppKey(String userId, String AppKey) throws SQLException {
String status = "false";
int count = 0;
ResultSet rs = null;

try {
checkConnection();
pstmntAppKeyValidate.setString(1, userId.trim());
pstmntAppKeyValidate.setString(2, AppKey.trim());
rs = pstmntAppKeyValidate.executeQuery();
if (rs != null) {
while (rs.next()) {
count = rs.getInt(1);
}
}
if (count > 0) {
status = "true";
}

} catch (Exception e) {
status = "ERROR";
System.out.println("***********************Exception is*******" + e.getMessage());
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return status;

}

/*public static String validateAppKey(String userId, String AppKey) {
String status = "false";
int count = 0;
ResultSet rs = null;
JDBCConnection instance = null;
PreparedStatement pstmntAppKeyValidate = null;
try {
instance = new JDBCConnection();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Connection con = instance.getConnection();

String query = "select count(1) from users_login_details where uld_entity_id = ? and uld_app_key = ? and trunc(uld_current_login_time) >= trunc(sysdate) and uld_invalid_login_flag in ('Y', 'C')";
try {
pstmntAppKeyValidate = con.prepareStatement(query);

pstmntAppKeyValidate.setString(1, userId.trim());
pstmntAppKeyValidate.setString(2, AppKey.trim());
rs = pstmntAppKeyValidate.executeQuery();
if (rs != null) {
while (rs.next()) {
count = rs.getInt(1);
}
}
if (count > 0) {
status = "true";
}
} catch (SQLException e) {
status = "ERROR";
System.out.println("***********************Exception is*******" + e.getMessage());
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
}
if(pstmntAppKeyValidate != null) {
pstmntAppKeyValidate.close();
}
if(con != null) {
con.close();
}
}catch(Exception e) {
e.printStackTrace();
}

}

return status;
}*/

private static synchronized void checkConnection() {
try {
if ((System.currentTimeMillis() - lastAccess) >= 120000) {
stmt = con.createStatement();
}
} catch (Exception g) {
try {
// instance = new JDBCConnection();
con = instance.getConnection();
pstmntAppKeyValidate = con.prepareStatement(strappKeyValidate);
// lastAccess = System.currentTimeMillis();
} catch (Exception m) {
}
}
lastAccess = System.currentTimeMillis();
}


}

当有多个用户时,我在生产中收到卡住的线程消息。制作同步 validateAppKey 方法而不是实例方法好吗?

最佳答案

删除 validateAppKey 方法上的静态,因为它会导致类级别的锁定。

关于java - 服务器中的线程卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com