从Session中获取Connection需要注意的
虽然不推荐从Session中获取connection,但是偶尔会出现一些特殊情况不得以这样做。
接下来的问题就是:使用后的session/connection如何处理?
1.close connection
相信大多数人的第一返回会是这样,关闭连接,这是非常错误的(没来得及看官方文档,自己做了下测试)
<property name="initialSize" value="1" />
<property name="maxActive" value="1" />
<property name="maxIdle" value="1" />
<property name="minIdle" value="1" />
</bean>
/**多线程测试关闭连接*/public static void closeTest() throws SQLException{Session session = Test.getSession();Connection conn = null;PreparedStatement pst = null;try{conn = session.connection();pst = conn.prepareStatement(sql);pst.executeQuery();}catch(Exception e){e.printStackTrace();}finally{conn.close();pst.close();}}
public static void poolTest() throws SQLException{getConnection().close();}
public static void closeSessionTest() throws SQLException{Session session = Test.getSession();Connection conn = null;PreparedStatement pst = null;try{conn = session.connection();pst = conn.prepareStatement(sql);pst.executeQuery();}catch(Exception e){e.printStackTrace();}finally{session.close();if(pst != null){pst.close();}}}
public static void closeSessionTest() throws SQLException{Session session = Test.getSession();Connection conn = null;PreparedStatement pst = null;try{conn = session.connection();pst = conn.prepareStatement(sql);pst.executeQuery();}catch(Exception e){e.printStackTrace();}finally{session.disconnect();if(pst != null){pst.close();}}}
if ( rootSession == null ) {return jdbcContext.getConnectionManager().close();}