Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
liugddx committed Feb 15, 2024
1 parent 4a6fdfa commit ff6e492
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;

public class SessionVariableField implements Serializable {
private transient Field field;
Expand Down Expand Up @@ -53,14 +54,32 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE

private Field getField(String fieldName, Class<?> fieldType) throws NoSuchFieldException {
try {
return SessionVariableField.class.getDeclaredField(fieldName);
return SessionVariable.class.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
Class<?> superclass = SessionVariableField.class.getSuperclass();
Class<?> superclass = SessionVariable.class.getSuperclass();
if (superclass != null) {
return getField(fieldName, fieldType);
}
throw e;
}
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
SessionVariableField other = (SessionVariableField) obj;
// 忽略 transient 字段的比较
return Objects.equals(this.getField(), other.getField());
}

@Override
public int hashCode() {
return Objects.hashCode(this.getField());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,16 @@ public void testForwardQueryOptions() {
Assertions.assertEquals(123, sessionVariable.getQueryTimeoutS());
Assertions.assertEquals(123, sessionVariable.getInsertTimeoutS());
}

@Test
public void testCloneSessionVariablesWithSessionOriginValueNotEmpty() throws NoSuchFieldException {
Field txIsolation = SessionVariable.class.getField("txIsolation");
SessionVariableField txIsolationSessionVariableField = new SessionVariableField(txIsolation);
sessionVariable.addSessionOriginValue(txIsolationSessionVariableField, "test");

SessionVariable sessionVariableClone = VariableMgr.cloneSessionVariable(sessionVariable);

Assertions.assertEquals("test",
sessionVariableClone.getSessionOriginValue().get(txIsolationSessionVariableField));
}
}

0 comments on commit ff6e492

Please sign in to comment.