Fix issues with caching

remotes/1721272053111006915/tmp_refs/heads/master
Charitha Goonetilleke 6 months ago
parent fb79d61b60
commit d1fe1baad0

@ -69,9 +69,20 @@ public class DeviceIdentifier implements Serializable{
@Override @Override
public String toString() { public String toString() {
return "deviceId {" + return type + "|" + id;
"id='" + id + '\'' +
", type='" + type + '\'' +
'}';
} }
@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 deviceId;
private String deviceType; private String deviceType;
private int tenantId; private int tenantId;
private volatile int hashCode;
public String getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
@ -55,27 +54,21 @@ public class DeviceCacheKey {
} }
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (obj == null) { return toString().hashCode();
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;
} }
@Override @Override
public int hashCode() { public String toString() {
if (hashCode == 0) { return tenantId + "|" + deviceType + "|" + deviceId;
hashCode = Objects.hash(deviceId, deviceType, tenantId); }
@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 io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.dao.impl.GenericHeartBeatDAOImpl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.IllegalTransactionStateException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; 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 javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
@ -42,7 +40,7 @@ public class HeartBeatBeaconDAOFactory {
private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOFactory.class); private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOFactory.class);
private static DataSource dataSource; private static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<>(); private static final ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
/** /**
* Get instance of GroupDAO * Get instance of GroupDAO
@ -64,7 +62,6 @@ public class HeartBeatBeaconDAOFactory {
/** /**
* Begin transaction with datasource for write data * Begin transaction with datasource for write data
* *
* @throws TransactionManagementException
*/ */
public static void beginTransaction() throws TransactionManagementException { public static void beginTransaction() throws TransactionManagementException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
@ -85,7 +82,6 @@ public class HeartBeatBeaconDAOFactory {
/** /**
* Open connection to the datasource for read data * Open connection to the datasource for read data
* *
* @throws SQLException
*/ */
public static void openConnection() throws SQLException { public static void openConnection() throws SQLException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
@ -102,7 +98,6 @@ public class HeartBeatBeaconDAOFactory {
* Get current connection to datasource * Get current connection to datasource
* *
* @return current connection * @return current connection
* @throws SQLException
*/ */
public static Connection getConnection() throws SQLException { public static Connection getConnection() throws SQLException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
@ -189,7 +184,7 @@ public class HeartBeatBeaconDAOFactory {
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList = List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties(); jndiConfig.getJndiProperties();
if (jndiPropertyList != null) { if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>(); Hashtable<Object, Object> jndiProperties = new Hashtable<>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue()); jndiProperties.put(prop.getName(), prop.getValue());
} }

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

@ -47,4 +47,23 @@ public class OperationTemplateCacheKey {
public void setDeviceType(String deviceType) { public void setDeviceType(String deviceType) {
this.deviceType = 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; 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; import java.util.concurrent.TimeUnit;
public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
private static final Log log = LogFactory.getLog(DeviceSubTypeServiceImpl.class); private static final Log log = LogFactory.getLog(DeviceSubTypeServiceImpl.class);
private static final LoadingCache<DeviceSubTypeCacheKey, DeviceSubType> deviceSubTypeCache private static final LoadingCache<DeviceSubTypeCacheKey, DeviceSubType> deviceSubTypeCache
= CacheBuilder.newBuilder() = CacheBuilder.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES) .expireAfterWrite(15, TimeUnit.MINUTES)
@ -143,7 +145,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
} }
@Override @Override
public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) public synchronized DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType)
throws SubTypeMgtPluginException { throws SubTypeMgtPluginException {
try { try {
DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType); 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 DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB; )ENGINE = InnoDB;
CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); 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_DEVICE_IDENTIFICATION ON DM_ENROLMENT(DEVICE_IDENTIFICATION);
CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS); CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS);

Loading…
Cancel
Save