Fix issues with caching

appm_improvement
Charitha Goonetilleke 7 months ago
parent fb79d61b60
commit d1fe1baad0

@ -69,9 +69,20 @@ public class DeviceIdentifier implements Serializable{
@Override
public String toString() {
return "deviceId {" +
"id='" + id + '\'' +
", type='" + type + '\'' +
'}';
return type + "|" + id;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof DeviceIdentifier) {
return (this.hashCode() == obj.hashCode());
}
return false;
}
}

@ -28,7 +28,6 @@ public class DeviceCacheKey {
private String deviceId;
private String deviceType;
private int tenantId;
private volatile int hashCode;
public String getDeviceId() {
return deviceId;
@ -55,27 +54,21 @@ public class DeviceCacheKey {
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!DeviceCacheKey.class.isAssignableFrom(obj.getClass())) {
return false;
}
final DeviceCacheKey other = (DeviceCacheKey) obj;
String thisId = this.deviceId + "-" + this.deviceType + "_" + this.tenantId;
String otherId = other.deviceId + "-" + other.deviceType + "_" + other.tenantId;
if (!thisId.equals(otherId)) {
return false;
}
return true;
public int hashCode() {
return toString().hashCode();
}
@Override
public int hashCode() {
if (hashCode == 0) {
hashCode = Objects.hash(deviceId, deviceType, tenantId);
public String toString() {
return tenantId + "|" + deviceType + "|" + deviceId;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof DeviceCacheKey) {
return (this.hashCode() == obj.hashCode());
}
return hashCode;
return false;
}
}

@ -24,10 +24,8 @@ import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.config.datasourc
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.dao.impl.GenericHeartBeatDAOImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.IllegalTransactionStateException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.UnsupportedDatabaseEngineException;
import javax.sql.DataSource;
import java.sql.Connection;
@ -42,7 +40,7 @@ public class HeartBeatBeaconDAOFactory {
private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOFactory.class);
private static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
private static final ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
/**
* Get instance of GroupDAO
@ -64,7 +62,6 @@ public class HeartBeatBeaconDAOFactory {
/**
* Begin transaction with datasource for write data
*
* @throws TransactionManagementException
*/
public static void beginTransaction() throws TransactionManagementException {
Connection conn = currentConnection.get();
@ -85,7 +82,6 @@ public class HeartBeatBeaconDAOFactory {
/**
* Open connection to the datasource for read data
*
* @throws SQLException
*/
public static void openConnection() throws SQLException {
Connection conn = currentConnection.get();
@ -102,7 +98,6 @@ public class HeartBeatBeaconDAOFactory {
* Get current connection to datasource
*
* @return current connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
Connection conn = currentConnection.get();
@ -189,7 +184,7 @@ public class HeartBeatBeaconDAOFactory {
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
Hashtable<Object, Object> jndiProperties = new Hashtable<>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}

@ -59,9 +59,8 @@ public class OperationTemplateCacheLoader extends CacheLoader<String, OperationT
String deviceType = operationTemplateCacheKey.getDeviceType();
if (log.isTraceEnabled()) {
log.trace(
"Loading operation template for subtype Id : " + subTypeId + " & deviceType : " + deviceType + " operation code : "
+ operationCode);
log.trace("Loading operation template for subtype Id : " + subTypeId
+ " & deviceType : " + deviceType + " operation code : " + operationCode);
}
try {
ConnectionManagerUtils.openDBConnection();

@ -47,4 +47,23 @@ public class OperationTemplateCacheKey {
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public String toString() {
return subTypeId + "|" + deviceType + "|" + operationCode;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof OperationTemplateCacheKey) {
return (this.hashCode() == obj.hashCode());
}
return false;
}
}

@ -58,4 +58,12 @@ public class DeviceSubTypeCacheKey {
return tenantId + "|" + subTypeId + "|" + deviceType;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof DeviceSubTypeCacheKey) {
return (this.hashCode() == obj.hashCode());
}
return false;
}
}

@ -42,7 +42,9 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
private static final Log log = LogFactory.getLog(DeviceSubTypeServiceImpl.class);
private static final LoadingCache<DeviceSubTypeCacheKey, DeviceSubType> deviceSubTypeCache
= CacheBuilder.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES)
@ -143,7 +145,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
}
@Override
public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType)
public synchronized DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType)
throws SubTypeMgtPluginException {
try {
DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType);

@ -133,7 +133,7 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB;
CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID);
CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE_ID ON DM_ENROLMENT(DEVICE_TYPE_ID);
CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE ON DM_ENROLMENT(DEVICE_TYPE);
CREATE INDEX IDX_ENROLMENT_DEVICE_IDENTIFICATION ON DM_ENROLMENT(DEVICE_IDENTIFICATION);
CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS);

Loading…
Cancel
Save