Tuesday 18 February 2020

Monday 17 February 2020

sql server temporary table cannot be found

jdbcTemplate.execute("sql to create #temp_table_1");

fun(){
   jdbcTemplate.execute("sql to update data in #temp_table_1")
}

Exception: Object temp_table_1 cannot be found.

This is may cause the temp_table_1 is created by connection 1, but update sql using connection 2. As local temporary table is created by one connection and cannot be found by another connection.

One solution here is using the same connection to create, retrive and update the temporary table,
Or create a global temporary table ##temp_table and then it can be found by different connection instance.

And if you using datasource and conneciton pool, normally you need to drop the temporary table emplicitely cause the connection will not be closed but returned to the pool.

Thursday 6 February 2020

getClass info may not correct when proxied (in Spring framework)

Say we have a class MyClass.java, and its instance myObject, normally,
myObject.getClass() and MyClass.class are two ways we get the class info, but sometimes the first one does not work when the instance is a proxy not the original one.

When we use Spring framework, if the instance is a Spring bean and been proxied already, the .getClass() may not return a correct Class object but the proxy one, so using MyClass.class as your first choice.