简体   繁体   English

通过c3p0在mysql中查询超时

[英]query timeout in mysql via c3p0

I am using c3p0 as connection pooling and ebean as ORM implementation. 我使用c3p0作为连接池,使用ebean作为ORM实现。 Currently i am setting below properties in c3p0 pool: 目前,我在c3p0池中设置以下属性:

minPoolSize
maxPoolSize
maxIdleTime
preferredTestQuery

But i was wandering how to set a timeout on the query ie suppose if the query take more than 200 milisec then just give up rather than waiting on it infinitely. 但是我在徘徊如何在查询上设置超时,即假设查询时间超过200毫秒,那么就放弃而不是无限期地等待它。 How would you achieve this via c3p0. 您将如何通过c3p0实现这一目标。 I know how to set this by plain JDBC ie Statement.setQueryTimeout() but how to do the same via c3p0. 我知道如何通过简单的JDBC即Statement.setQueryTimeout()进行设置,但如何通过c3p0进行相同的设置。 Or is there any other way? 还是还有其他方法?

is maxIdleTime and query timeout that i have mentioned does the same thing ie after certain period of time if we dont get the response kill the connection? 是我提到的maxIdleTime和查询超时是否做同样的事情,即在一定时间后,如果我们没有得到响应,就会maxIdleTime连接? Thanks in advance. 提前致谢。

No, maxIdleTime has nothing to do with how long a query can run. 不, maxIdleTime与查询可以运行多长时间无关。 It defines how long Connections will be allowed to sit idle in the pool before they will expire and be destroyed. 它定义了连接在过期和被销毁之前将被允许在池中闲置多长时间。

c3p0 is plain JDBC. c3p0是纯JDBC。 Per the JDBC spec, it offers transparent Connection pooling. 根据JDBC规范,它提供透明的连接池。 Transparent means in this context that the API doesn't change, your code should be basically the same with or without the pool. 透明意味着在这种情况下,API不会更改,带或不带缓冲池的代码应基本相同。 You are welcome to call Statement.setQueryTimeout() on Statements derived from a c3p0-managed Connection as you would with any other Statement. 与对其他任何语句一样,欢迎您对从c3p0托管的连接派生的Statement.setQueryTimeout()调用Statement.setQueryTimeout()

In general, c3p0 doesn't interfere with Connections while they are checked out. 通常,当检出连接时,c3p0不会干扰连接。 Once a client application is using a Connection, c3p0 observes but does not much mess with what the client app does. 客户端应用程序使用Connection之后,c3p0会观察到但不会与客户端应用程序的行为发生太多混乱。 The main exception to this is the config param unreturnedConnectionTimeout , which is intended to help debug or workaround Connection leaks. 对此的主要例外是config参数unreturnedConnectionTimeout ,它旨在帮助调试或解决连接泄漏。 [See here ] In a real pinch, you could use unreturnedConnectionTimeout to do what you want, force abandonment of slower queries, albeit in a rather brutal way. [查看此处 ]实际情况是,您可以使用unreturnedConnectionTimeout来执行所需的操作,强制放弃较慢的查询,尽管这种方式相当残酷。 unreturnedConnectionTimeout will force the unreturned Connection to be close()ed after the set period of time, and the pool will usually have to acquire a replacement. 在设置的时间段后, unreturnedConnectionTimeout将强制未返回的Connection被关闭(),并且池通常将必须获得替换。 I'd start with Statement.setQueryTimeout before trying `unreturnedConnectionTimeout', though. 不过,在尝试“ unreturnedConnectionTimeout”之前,我先从Statement.setQueryTimeout开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM