Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CLOB:JdbcTemplate:c3p0-如何重用相同的连接?_Java_Spring_Oracle_C3p0_Clob - Fatal编程技术网

Java CLOB:JdbcTemplate:c3p0-如何重用相同的连接?

Java CLOB:JdbcTemplate:c3p0-如何重用相同的连接?,java,spring,oracle,c3p0,clob,Java,Spring,Oracle,C3p0,Clob,我继承了一个具有以下设置的项目: spring框架:3.2.2 oracle驱动程序:ojdbc6.jar:11.2.0.4 连接池的c3p0:0.9.1.2 它利用了动态数据源路由,我认为这是受本文启发的 声明的类和用于执行查询的方法之一如下所示 public class DataSourceServiceImpl extends SimpleJdbcDaoSupport implements DataSourceService { ... public List<Map<

我继承了一个具有以下设置的项目:

spring框架:3.2.2 oracle驱动程序:ojdbc6.jar:11.2.0.4 连接池的c3p0:0.9.1.2 它利用了动态数据源路由,我认为这是受本文启发的 声明的类和用于执行查询的方法之一如下所示

public class DataSourceServiceImpl extends SimpleJdbcDaoSupport implements DataSourceService {
...
    public List<Map<String, Object>> valueList(String dataSource, Object[] params, String sql) throws DataAccessException {
        DataSourceContextHolder.setDataSource(dataSource);
        return getSimpleJdbcTemplate().getJdbcOperations().queryForList(sql, params);
    }
}
这是有效的,但我担心这不是最佳的,只会在将来的某个时候引起问题


我的问题是,有没有办法重用用于为实际查询生成CLOB的连接?

最简单的方法是避开过于复杂的中间件

public List<Map<String, Object>> valueList(String dataSource, Object[] params, String sql) throws DataAccessException {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Clob clob = null;

    try {
      con = dataSource.getConnection();
      ps = con.prepareStatement( "SELECT samplefield FROM sampletable WHERE SDE.ST_INTERSECTS(SHAPE, SDE.ST_GEOMETRY(?, ?)) = 1 AND SHAPE IS NOT NULL" ); // probably externalize this SQL string somewhere

      // Note that Oracle's two-arg ST_GEOMETRY function takes a CLOB and an INTEGER
      // see http://resources.arcgis.com/en/help/main/10.1/index.html#//006z00000050000000
      Clob clob = OracleUtils.createTemporaryCLOB(conn, true, 10);                    
      clob.setString(1, (String) params[0]);
      ps.setClob( 1, clob );

      ps.setObject( 2, params[1], java.sql.Types.INTEGER );

      rs = ps.executeQuery();

      // inefficient and overdone output format, but hey.
      List<Map<String,Object>> out = new LinkedList()
      while (rs.next()) {
         Map<String,Object> oneBindingMap = new HashMap(1);
         oneBindingMap.put("samplefield", rs.getObject(1)); //again, maybe externalize the field name
         out.add( oneBindingMap );
      }
      return out;
    }
    catch ( SQLException e ) {
     throw new DataAccessException( e.getMessage(), e ); // adapt to expected Exception type
    }
    finally {
      try { if ( clob != null ) clob.free(); } catch ( Exception e ) { e.printStackTrace(); }

      // in java 7+ you could avoid the rest of this via the try-with-resources construct
      try { if ( rs != null ) rs.close(); } catch ( Exception e ) { e.printStackTrace(); }
      try { if ( ps != null ) ps.close(); } catch ( Exception e ) { e.printStackTrace(); }
      try { if ( con != null ) con.close(); } catch ( Exception e ) { e.printStackTrace(); }
    }
}
我只是把它输入一个网页;我还没有测试它是否编译,我正在对您想要的语义进行一些假设,例如输出映射中的格式和键名。但我认为这应该会让您非常接近您想要的位置,而不必费解您的逻辑以使其适应不打算做您想要做的事情的中间件

或者,我认为你的SingleConnectionDataSource黑客没有多少错误或可能失败。我觉得它是不透明的。我的解决方案要详细得多,但我认为逻辑上非常简单,对于任何理解JDBC的人来说都可以进行维护


祝你好运

感谢您的回复,我以前在preparedStatement中有过这个解决方案,但我并不真的想弄乱结果集,我希望保持结果集整洁,尽管最后我遇到了混乱,但感谢bindingmap:。不过,有没有办法在查询操作中告诉jdbctemplate使用我已有的连接?或者任何对我有帮助的地方。。就像过去有挫折感或类似的东西一样。。。。
JdbcTemplate t = new JdbcTemplate(new SingleConnectionDataSource(conn, false));
result = t.queryForList(sql, params);
public List<Map<String, Object>> valueList(String dataSource, Object[] params, String sql) throws DataAccessException {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Clob clob = null;

    try {
      con = dataSource.getConnection();
      ps = con.prepareStatement( "SELECT samplefield FROM sampletable WHERE SDE.ST_INTERSECTS(SHAPE, SDE.ST_GEOMETRY(?, ?)) = 1 AND SHAPE IS NOT NULL" ); // probably externalize this SQL string somewhere

      // Note that Oracle's two-arg ST_GEOMETRY function takes a CLOB and an INTEGER
      // see http://resources.arcgis.com/en/help/main/10.1/index.html#//006z00000050000000
      Clob clob = OracleUtils.createTemporaryCLOB(conn, true, 10);                    
      clob.setString(1, (String) params[0]);
      ps.setClob( 1, clob );

      ps.setObject( 2, params[1], java.sql.Types.INTEGER );

      rs = ps.executeQuery();

      // inefficient and overdone output format, but hey.
      List<Map<String,Object>> out = new LinkedList()
      while (rs.next()) {
         Map<String,Object> oneBindingMap = new HashMap(1);
         oneBindingMap.put("samplefield", rs.getObject(1)); //again, maybe externalize the field name
         out.add( oneBindingMap );
      }
      return out;
    }
    catch ( SQLException e ) {
     throw new DataAccessException( e.getMessage(), e ); // adapt to expected Exception type
    }
    finally {
      try { if ( clob != null ) clob.free(); } catch ( Exception e ) { e.printStackTrace(); }

      // in java 7+ you could avoid the rest of this via the try-with-resources construct
      try { if ( rs != null ) rs.close(); } catch ( Exception e ) { e.printStackTrace(); }
      try { if ( ps != null ) ps.close(); } catch ( Exception e ) { e.printStackTrace(); }
      try { if ( con != null ) con.close(); } catch ( Exception e ) { e.printStackTrace(); }
    }
}