Java ORA-12518,TNS:侦听器无法切换客户端连接,该连接来自内存访问量大的循环

Java ORA-12518,TNS:侦听器无法切换客户端连接,该连接来自内存访问量大的循环,java,oracle,Java,Oracle,我有一个从oracle访问大量内存的循环 int firstResult = 0; int maxResult = 500; int targetTotal = 8000; // more or less int phase = 1; for (int i = 0; i<= targetTotal; i += maxResult) { try { Session session = .... init hib

我有一个从oracle访问大量内存的循环

    int firstResult = 0;
    int maxResult = 500;
    int targetTotal = 8000; // more or less

    int phase = 1;
    for (int i = 0; i<= targetTotal; i += maxResult) {
        try {
            Session session = .... init hibernate session ...
            // Start Transaction
            List<Accounts> importableInvAcList = ...getting list using session and firstResult-maxResult...

            List<ContractData> dataList = new ArrayList<>();
            List<ErrorData> errorDataList = new ArrayList<>();

            for (Accounts account : importableInvAcList) {
                ... Converting 500 Accounts object to ContractData object ...
                ... along with 5 more database call using existing session ...
                .. On converting The object we generate thousands of ErrorData...

                dataList.add(.. converted account to Contract data ..);
                errorDataList.add(.. generated error data ..);
            }

            dataList.stream().forEach(session::save); // 500 data

            errorDataList.stream().forEach(session::save); // 10,000-5,000 data

            ... Commit Transaction ...
            phase++;
        } catch (Exception e) {

            return;
        }
    }
在第二阶段,样本的状态如下

已使用:1022MB,免费:313MB,总分配:1335MB

堆栈跟踪在此…

    org.hibernate.exception.GenericJDBCException: Cannot open connection
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
        at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
        at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
        at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
        at ibbl.remote.tx.TxSessionImpl.beginTx(TxSessionImpl.java:41)
        at ibbl.remote.tx.TxController.initPersistence(TxController.java:70)
        at com.ibbl.data.util.CDExporter2.run(CDExporter2.java:130)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12518, TNS:listener could not hand off client connection
注意,这个进程运行在一个
线程
,一次运行3个类似的
线程
为什么循环运行一段时间后此异常会挂起?

一次运行3个类似的线程

如果您的代码总共创建了3个线程,那么最好只需要3个Oracle连接。在创建任何线程之前创建所有线程。创建线程,为每个线程分配一个连接,然后启动线程


不过,很有可能您的代码在托管它的任何机器上都会过度消耗资源。即使您消除了ORA-12518,RDBMS服务器也可能“南下”。所谓“往南走”,我的意思是,如果您的应用程序消耗了太多的资源,托管它的机器或托管RDBMS服务器的机器可能会“恐慌”或其他同样可怕的事情。

这是什么“…初始化hibernate会话…”做的?是否为循环的每个迭代创建到数据库的新连接?为什么不在循环之外初始化会话呢?@a_horse_和_no_name每个循环几乎需要30秒才能完成。这就是我在循环中生成会话并在其中提交/关闭的原因。发生这种情况有什么问题吗?最好保持连接打开(假设与“会话”相同)。不断地打开和关闭一个(物理)连接不是一个好主意(除非你在后台有一个连接池)@a_horse_和_no_name抱歉。。我已经尝试将会话保持在循环之外,但同样的问题也出现了。每个线程可能需要30分钟才能完成。在这么长的时间内保持会话的活动性有什么问题吗?我已经看到了会话持续工作超过24小时的痕迹。但你可能应该澄清你所说的“保持活力”是什么意思。这是一个特别的短语,我想确保你在进一步评论之前所说的话是认真的。我的意思是“让会议长时间开放”。不过,谢谢你的建议。我认为会议不应该持续很长时间。现在我已经在循环外实例化了会话。谢谢
    org.hibernate.exception.GenericJDBCException: Cannot open connection
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
        at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
        at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
        at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
        at ibbl.remote.tx.TxSessionImpl.beginTx(TxSessionImpl.java:41)
        at ibbl.remote.tx.TxController.initPersistence(TxController.java:70)
        at com.ibbl.data.util.CDExporter2.run(CDExporter2.java:130)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12518, TNS:listener could not hand off client connection