Reputation: 3562
My java class is this
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();
}
}
I am getting stuck thread message on production when there were multiple users . is it good to make synchronized validateAppKey method instead of instance method ?
Upvotes: 0
Views: 373
Reputation: 3562
This problem can be solved by using thread safety (i.e synchronized method )
Upvotes: 0
Reputation: 143
Remove static on validateAppKey method because it causes the lock on class level.
Upvotes: 1