tomcat 上的 maxConnections 或 maxThreads

tomcat 上的 maxConnections 或 maxThreads

尋求建議 - 我已閱讀有關此問題的其他兩個主題

在我的 server.xml 檔案中,我有兩個地方定義了 maxThreads:

  1. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="100" minSpareThreads="4"/>

  1. <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="100" SSLEnabled="true" scheme="https" secure="true" connectionTimeout="600000" keystoreFile="/usr/local/tomcat/conf/keystore.p12" keystorePass="mypassword" clientAuth="false" sslProtocol="TLS" />

我們的伺服器經常遇到的錯誤是:「逾時:池為空。無法在 30 秒內取得連接,沒有可用的連接 [size:100;busy:100;idle:0;lastwait:30000]」系統關閉(機器重置並再次啟動- 在AWS ECS 叢集上)

當我在此處列出的第二個實例中將 maxThreads 值增加到 300 時,我們收到相同的錯誤訊息 - 因此我不確定連接大小是否已增加。系統行為不同(機器不重新啟動),但使用者無法連線 - 最終需要手動重新啟動。

如何實現與系統的更多連接或保持盡可能高的連接性?

在有關此主題的其他帖子中,一些人建議減少 maxThreads(假設它們很快完成)可以提供更好的性能。

更新:

在我的應用程式屬性檔案中,我有以下設定:

spring.datasource.url=jdbc:postgresql://db####
spring.datasource.username=#####
spring.datasource.password=######
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=60
spring.datasource.tomcat.test-on-borrow=true

spring.jpa.show-sql=false
#spring.jpa.hibernate.ddl-auto=create-drop
#spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

spring.jpa.hibernate.connection.provider_class=org.hibernate.c3p0.internal.C3P0ConnectionProvider
spring.jpa.properties.hibernate.c3p0.min_size=1
spring.jpa.properties.hibernate.c3p0.max_size=30
spring.jpa.properties.hibernate.c3p0.timeout=120
spring.jpa.properties.hibernate.c3p0.max_statements=20

答案1

您的應用程式遭受資料庫連線池耗盡的困擾:由於 Tomcat 中的工作執行緒 (100) 多於連線池中的可用連線 (60),因此許多執行緒需要等待可用連線。您的資料庫連線數至少應與工作執行緒數相同。嘗試使用:

spring.datasource.tomcat.max-active=200

評論:由於您<Connector>沒有executor屬性,因此您建立的屬性<Executor>不會被使用並且可以被刪除(除非另一個連接器使用它)。

由於沒有spring.jpa.hibernate.connection.provider_class屬性,您嘗試設定的 C3P0 連線池永遠不會建立:Hibernate 將使用透過屬性配置的連線池spring.datasource.*。因此,您可以刪除與 C3P0 相關的屬性。

相關內容