我有一个用grails开发的应用程序(1.1.1)。我在datasource.groovy代码片段中使用了一个hibernate启用池化的MySQL服务器配置数据源作为folows -
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
environments {
development {
dataSource {
logSql = true
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:mysql://<dev-server-ip>:3306/<db-name>"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://<test-server-ip>:3306/<db-name>"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://<prod-server-ip>:3306/<db-name>"
}
}
用于连接到另一个MS SQL Server 2005.我已经定义了一个具有以下连接设置的服务类
def connection = Sql.newInstance(
"jdbc:sqlserver://<mssql-db-server-ip>;databaseName=<db-name>",
'<username>',
'<password>',
'com.microsoft.sqlserver.jdbc.SQLServerDriver')
我知道这个连接不是池连接。每当我使用此连接检索数据时创建会话。那么,是否有任何机制可以在服务类中创建池连接?
当我使用此ms sql服务器连接从服务类运行查询时,选择数据查询,如
String pullData() {
def userId = "sample_user"
def testData = connection.firstRow("select distinct (COLUMN_NAME1+' > '+replace(COLUMN_NAME2,' > ', '>')+' > '+COLUMN_NAME3+' > '+COLUMN_NAME4+' > '+EMAIL) testRow from VIEW_NAME where lower(substring(EMAIL,1,(select charindex('@',EMAIL))-1 )) = lower(?)",[userId])
def myStringList = testData.testRow.split(' > ')
def myTestDataList = myStringList[4].split('@')
def email = myTestDataList[0]
return email
}
我有一个例外。堆栈跟踪是
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.reflect.UndeclaredThrowableException
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at jcifs.http.NtlmHttpFilter.doFilter(NtlmHttpFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.reflect.UndeclaredThrowableException
at PullDataService$$EnhancerByCGLIB$$881a39b3.pullData(<generated>)
at PullDataService$$FastClassByCGLIB$$f8a1c77f.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
... 33 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Connection timed out
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1368)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1355)
at com.microsoft.sqlserver.jdbc.TDSChannel.write(IOBuffer.java:1548)
at com.microsoft.sqlserver.jdbc.TDSWriter.flush(IOBuffer.java:2368)
at com.microsoft.sqlserver.jdbc.TDSWriter.writePacket(IOBuffer.java:2270)
at com.microsoft.sqlserver.jdbc.TDSWriter.endMessage(IOBuffer.java:1877)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:4403)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:386)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:281)
at PullDataService.pullData(PullDataService.groovy:38)
at PullDataService$$FastClassByCGLIB$$7ff09ca7.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
... 40 more
请帮助我,我很长一段时间都在忍受它。任何建议都值得赞赏。提前谢谢。
答案 0 :(得分:0)
我可以看到一些会降低此查询速度的事情。主要是基于一堆不同的字段创建一个字符串列,并在其上调用distinct。不需要区别,因为您只使用第一个结果而不需要其他列,因为您只关心电子邮件。请考虑这样的事情:
def userId = "sample_user".toLowerCase()
def testData = connection.firstRow("select top 1 EMAIL as email" +
" from VIEW_NAME" +
" where lower(EMAIL) like ?",[userId + "@%"])
def email = testData?.email?.split('@')[0]
return email
由于类似的条款,它仍然可能很慢,但应该比使用distinct更快。
您可能需要考虑在视图/基础表中添加一列,以便在电子邮件地址列中包含单独的用户ID列。