--- title: ORA-12518, TNS:listener could not hand off client connection (監聽程式無法分發客戶機連線錯誤) 解決方法 tags: Oracle,SQLexception --- ORA-12518, TNS:listener could not hand off client connection (監聽程式無法分發客戶機連線錯誤) 解決方法 === ###### tags:`Oracle` `SQLexception` > 程式撰寫時,**沒有良好的開啟/關閉習慣,常造成GC問題** (Garbage Collection) > 一旦處理過大量資料且匯入DataBase時,各種Error msg就會排山倒海而來 **(SQLException)** ## :memo: 釐清問題 造成ora-12518錯誤回報有分很多種 :arrow_right: [ora-12518問題種類](https://support.oracle.com/knowledge/Oracle%20Database%20Products/556428_1.html) 造成問題有太多可能性,必須一步步解析才能得到最核心的錯誤訊息 --- ### Solution--001 : 問題回報 Listener 處理 - 遠端連接/啟動 Database 電腦 - 開啟 cmd - 輸入資料 ``` C:\Users\Administartor> sqlplus/nolog SQL>connect sys/test as sysdba 已連線 ``` #### :arrow_up: **到這沒問題,接下來可以盤查下一步** ``` SQL>connect sys/test@test as sysdba ERROR: ORA-12518: TNS: listener could not hand off client connection ``` :exclamation:當錯誤回報出現 ora-12518:Listener :arrow_right: 代表監聽器有問題需要立即處理 --- 根據問題查找相關資料如下:[DataBase ORA-12518 問題](http://www.dba-oracle.com/sf_ora_12518_tns_listener_could_not_hand_off_client_connection.htm) #### 錯誤訊息: ORA-12518: TNS:listener could not hand off client connection Cause: The process of handing off a client connection to another process failed. Action: Turn on listener tracing and re-execute the operation. Verify that the listener and database instance are properly configured for direct handoff. If problem persists, call Oracle Support. 根據上文處理方式,只要重新啟動 Oracle server / listener 即可 **cmd+R :arrow_right: services.msc :arrow_right: oracleService / OracleTNSListener <兩者重新啟動即可>** --- ### Solution--002 : 增加參數 DISPATCHERS 連接 若通過 **Solution--001** 無法解決時,可以參考:arrow_right:[ORA-12518 Dispatchers](https://blog.csdn.net/linruichao/article/details/8537237) 並且接下來採用以下列方法: - 遠端連接/啟動 Database 電腦 - 開啟 cmd - 輸入資料 ``` >C:\Users\Administartor> sqlplus/nolog SQL>connect sys/test as sysdba 已連線 SQL>show parameter; NAME TYPE VALUE ------------------------- ------------------ ---------------------- aq_tm_processes integer 0 db_writer_processes integer 1 gcs_server_processes integer 0 job_queue_processes integer 10 log_archive_max_processes integer 2 processes integer 300 SQL>select (*) from v$session; COUNT(*) ---------- 124 ``` 從上述查找資料中,**processes** 進程總數為 300,並且在使用中的為 **124列** :exclamation:表示問題不在於Processes進程數量處理上 --- 因此可以判定為 DISPATCHERS 出了設定上的問題 ``` SQL> select (*) from v$parameter where name = 'dispatchers'; NAME TYPE ------------------------------------ ---------------------- VALUE ------------------------------ dispatchers string (PROTOCOL=TCP) (SERVICE=cmc3dx dbXDB) ``` Service部分連接錯誤的DataBase資料,必輸改寫成正確的 <**若不曉得去查log錯誤回報**> ``` <!-- 示範:我的資料庫正確為 hostname:localhost port:1521 SID:orcl2xdb --> SQL> alter system set DISPATCHERS = '(protocol=tcp)(dispatchers)(service=orcl2xdb)' system altered SQL>show value from v$session where name = 'dispatchers'; VALUE ------------------------------ (PROTOCOL=TCP) (SERVICE=orcl2xdb) ``` 以上設定完成,就可以解決資料庫連接錯誤的問題。 --- ### Solution--003 : 增加連接 Connect 連接埠 當以上方式皆無法應對問題時,則必須思考是不是process進程數量是否短缺 排解問題的方法如下: - 遠端連接/啟動 Database 電腦 - 開啟 cmd - 輸入資料 ``` C:\Users\Administartor> sqlplus/nolog SQL>connect sys/test as sysdba ERROR: ORA-00020: maximum number of processes (300) exceeded ``` 在connect sys/test as sysdba時就產生報錯問題,則問題產生在於processes身上 所以先重新關閉 / 啟動資料庫,消除多餘的連接埠,才能重新連上線 ``` SQL> show parameter processes NAME TYPE VALUE -------- -------- -------- ocesses integer 300 SQL> select count(*) from v$process; COUNT(*) ----------------------- 300 SQL> alter system set processes=500 scope=spfile; System altered. SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup ORACLE instance started. SQL> show parameter processes NAME TYPE VALUE -------- -------- -------- ocesses integer 500 ``` 以上方法可以改變processes數量上限,注意:exclamation:要確實的關閉/重啟資料庫 >進程上是不是有無形的程式在消耗資源,這點要確認清楚 > >一般安裝數據庫 **processes** 默認數量為150,在大型應用上需要注意小心 --- ### Solution--004 : 檢查使用中的程式碼 以上的方案皆採用過後,仍持續 **產生ora-12518問題** 則代表一件事情! 有無形的東西在消耗Database的process數量 首先確認使用中的processes數量: ``` SQL>SELECT upper(resource_name) as resource_name,current_utilization,max_utilization,initial_allocation 2>FROM v$resource_limit WHERE resource_name in ('processes', 'sessions'); RESOURCE_NAME CURRENT_UTILIZATION -------------------- -------------------------- MAX_UTILIZATION INITIAL_ALLOCATION -------------------- -------------------------- PROCESSES 126 134 300 SESSIONS 133 143 472 ``` 返回程式碼,確實查看process是否有正確關閉 ```java= process proc = new process(); ...... proc.close(); ...... Connection connection = null; Statement statement = null; try(){ ....... }catch{ ....... }finally{ connection.close(); statement close(); } ``` :exclamation: 利用finally特性,使try/catch上何者成立,最後finally皆處理 --- ### :computer: 連結 <div class="link-Table"> | 參考網站 | 連結 | |:------------------------------------- |:-----------------------------------------------:| | ORA-12518 TNS錯誤 | [:link:][ORA-12518 TNS錯誤] | | ORA-00020 超載最大Process(300) | [:link:][ORA-00020 超載最大Process(300)] | | ORA-認識 tnsnames.ora / listener.ora | [:link:][ORA-認識 tnsnames.ora / listener.ora] | | ORA-12518 / TNS-12518 Troubleshooting | [:link:][ORA-12518 / TNS-12518 Troubleshooting] | | ORA-12518 processes increase | [:link:][ORA-12518 processes increase] | [ORA-12518 TNS錯誤]: http://ask.apelearn.com/question/610 [ORA-00020 超載最大Process(300)]:https://community.oracle.com/tech/developers/discussion/2471315/ora-00020-maximum-number-of-processes-300-exceeded [ORA-認識 tnsnames.ora / listener.ora]: https://www.youtube.com/watch?v=qiRqqqLNNG4 [ORA-12518 / TNS-12518 Troubleshooting]: https://www.huaweicloud.com/articles/62a6a3f59ad959e592f64e298f3d175a.html [ORA-12518 processes increase]: https://www.programmersought.com/article/2044772892/ </div> <style> div.link-Table > table th:nth-of-type(1) { width: 80vw; } div.link-Table > table th:nth-of-type(2) { width: 10vw; } div.moneyTable > table th:nth-of-type(1) { width: 20vw; } div.moneyTable > table th:nth-of-type(2) { width: 20vw; } div.moneyTable > table th:nth-of-type(3) { width: 40vw; } </style>