JSR 352:连接已关闭如果在分区步骤中的读取器close()中关闭连接,则会出错

时间:2016-07-22 13:27:44

标签: java websphere-liberty jsr352 java-batch

我在Reader打开时创建了一个Connection Connection con = ds.getConnection();(其中ds是DataSource),并在Reader的close()中关闭它。

但是当我运行多个分区的作业时,在作业中间,我得到Connection已关闭错误

Caused by: java.sql.SQLException: [jcc][t4][10335][10366][3.58.82] Invalid operation: Connection is closed. ERRORCODE=-4470, SQLSTATE=08003 DSRA0010E: SQL State = 08003, Error Code = -4,470

我认为当其中一个分区完成时会发生这种情况。

所以我的问题是为什么会发生这种情况?应该如何处理连接?或者Java是否负责关闭连接?

我在WebSphere Liberty上使用Java Batch 更新:     

<jdbcDriver libraryRef="DB2JCC4Lib"/>

<properties.db2.jcc databaseName="" driverType="4" password="" portNumber="" queryDataSize="65535" serverName="" user=""/>

</dataSource>




public class Reader implements ItemReader {
private DataSource ds = null;
private Connection con = null;
public Reader() {

}

public void close() {
    try {
        con.close();
        rs.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

/**
 * @see ItemReader#readItem()
 */
public Object readItem() {
    String s="";
    try {
    if (rs.next()) {
                for (int i = 1; i <= 10; i++) {
                    s+=rs.getString(i);                     
                }
                return s;
            }
         else {
            return null;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
public Serializable checkpointInfo() {

}

public void open(Serializable checkpoint) {

    if (ds == null) {
        try {
            ds = (DataSource) new InitialContext()
                    .lookup("java:comp/env/jdbc/dataSource");
        } catch (Exception e) {
        e.printStackTrace();
        }
    }

    try {
        con = ds.getConnection();
        statement= con
                .prepareCall("call abc.xyz(?)");
        statement.setString("param", "xxx");
        boolean result= statement.execute();
        if (result) {
            rs = statement.getResultSet();
            if (rs == null) {
                throw new NullPointerException();
            }               
        } else {
            throw new SQLException();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

完成错误讯息 [ERROR ] J2CA0024E: Method rollback, within transaction branch ID {XidImpl: formatId(57415344), gtrid_length(36), bqual_length(40), data(0000015645eff4470000000915937ff85f46c3ed056b19010aa5147e1183f8d3ae81c04c0000015645eff4470000000915937ff85f46c3ed056b19010aa5147e1183f8d3ae81c04c00000001)} of resource pool connectionManager[Pool], caught com.ibm.ws.exception.WsException: DSRA0080E: An exception was received by the Data Store Adapter. See original exception message: [jcc][t4][10335][10366][3.58.82] Invalid operation: Connection is closed. ERRORCODE=-4470, SQLSTATE=08003. with SQL State : 08003 SQL Code : -4470

1 个答案:

答案 0 :(得分:0)

我不知道JSR-352批处理的处理方式与Spring Batch完全相同,但是......

在Spring Batch中,如果你有一个使用块处理的Reader,我认为你可以做的就是将openConnection()放在beforeRead()closeConnection()afterRead()

要做到这一点,你应该实现一个监听器。检查这些,这样你就可以了解我在说什么。

Spring Annotation Type BeforeRead

Interface ItemReadListener