Cannot reach a temp table using Java SQL Statement object passed as a Map Value

元气小坏坏 提交于 2019-12-11 03:22:48

问题


My code currently works something similar to this

public void foo (Statement st, String sqlStr, String tempTableName) {
String aString = foo(tempTableName);



boolean result = st.execute(aString); // creates temp table

// Now do something else in another method
writeToTT(st, tempTableName);

}

public void writeToTT(Statement st, String tempTableName) {
// Final String name
String finalSqlWithTempTableName = foo(tempTableName);

st.execute(finalSqlWithTempTableName);
}

The above works fine. However, when I pass the Statement object as part of a HashMap it fails inside the writeToTT method

public void foo (Statement st, String sqlStr, String tempTableName) {
String aString = foo(tempTableName);

st.exectue(aString); // Creates temp table

// Now do something else in another method but pass it within a map
Map<String,Object> mapObj = new HashMap<>();
mapObj.put("statement",st);

writeToTT(mapObj, tempTableName);

}

public void writeToTT(Map<String,Object> mapObj, String tempTableName) {
// Final String name
String finalSqlWithTempTableName = foo(tempTableName);
Statement st = (Statement)mapObj.get("statement");
st.execute(finalSqlWithTempTableName); // Fails here saying that tempTableName object doesn't exist.

}

I used a debugger to check the identity Hash Code using System.identityHashCode(obj) - and both cases (inside and outside the method frame stack) the hash codes are the same. It's indeed referring to the same object using the reference value passed in paramMap.

I am just trying to understand whether this is because how it's being passed as a new object reference with HashMap or is it because the actual statement object doesn't have a valid hashCode override?

My assumption/understanding is that the temp table location in memory and the Statement object's access to that memory location changes as soon as I pass it via HashMap even though the object hashCode remains the same within writeToTT method. But may be I am mistaken?

来源:https://stackoverflow.com/questions/57380797/cannot-reach-a-temp-table-using-java-sql-statement-object-passed-as-a-map-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!