正常状况,咱们会调用存储过程用hibernate提供的链接池代理链接类来调用存储过程,而用新建链接给存储过程组拼STRUCT。spring
可是这样感受能够再一步的优化:调用存储过程与构建STRUCT用hibernate提供的同一个代理connection,可是这里须要用到一个connection的类型转换。由于struct不能用代理类组拼。优化
1 public Connection getConnection(Connection conn) { 2 C3P0NativeJdbcExtractor cp30NativeJdbcExtractor = new C3P0NativeJdbcExtractor(); 3 Connection con = null; 4 try { 5 con = cp30NativeJdbcExtractor 6 .getNativeConnection(conn); 7 } catch (SQLException e) { 8 e.printStackTrace(); 9 } 10 return con; 11 }
这样,实际上是使用了spring提供的一个转换类方法 C3P0NativeJdbcExtractor,他能够把代理类转化为原始类。 这样就能够节省新建链接所用的资源了。
而且,在组拼STRUCT时,不须要再关闭conn了,而是交由hibernate统一关闭。spa
在这个过程当中遇到一个代码异常:Unhandled exception type SQLException。 说明该代码有可能抛出异常,而且你没有经过try。。catch捕捉。 添加try。。catch就正常了hibernate