【问题标题】:How should I do a DB update using SQLJ in a Liberty Batch (JSR 352) ItemWriter?我应该如何在 Liberty Batch (JSR 352) ItemWriter 中使用 SQLJ 进行数据库更新?
【发布时间】:2017-11-10 23:23:02
【问题描述】:

我应该如何获取/缓存/关闭我的 ConnectionContext(连接)?

我是否应该在 ItemWriter.open() 中缓存上下文并跨每个块的 ItemWriter.writeItems()

缓存

如果我想使用特定于 DB2 的 API,该怎么办?

【问题讨论】:

    标签: websphere-liberty jsr352 java-batch


    【解决方案1】:

    推荐的方法通常是在每个块中运行的 writeItems() 方法中为 SQLJ 使用 get-use-close 模式。这比在 open() 中获取连接/上下文和跨块缓存更可取。

    您可以使用 Connection.unwrap() 方法进行特定于 JDBC 驱动程序的调用。

    比如:

    public class MyDB2SQLJItemWriter implements ItemWriter {
    
        DataSource myDataSource; // get via injection (not shown) or however
    
        // ...
    
        public void writeItems(List<Object items) throws Exception {
    
            Connection con = myDataSource.getConnection();
            DB2Connection db2Con = con.unwrap(DB2Connection.class);
            db2Con.setPackagePath(currentPackagePath);  // DB2-specific call
            SqljCtx sqljCtx = new SqljCtx(db2Con);
    
            // now do SQLJ
    
            // close SqljCtx AND DB2Connection
            sqljCtx.close();
        }
    

    也无需使用 enableConnectionCasting 属性配置您的数据源(这增加了此处不需要的灵活性,并为与连接缓存相关的更复杂问题打开了大门)。

    此建议扩展了一般建议,以在使用 JDBC API 时遵循 WebSphere 中的 get-use-close 模式。让 WebSphere 做连接池提供了良好的性能、更好的资源利用率并避免了一些更复杂的池场景。

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 1970-01-01
      • 2014-12-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多