2012-01-30 106 views
4

我使用spring 2.5 SimpleJdbcTemplate來訪問MySQL數據庫。當我嘗試訪問DB過於頻繁(使用Quartz來訪問它每分鐘)我得到這個堆棧跟蹤:Spring 2.5無法獲得JDBC連接

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.) 
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:458) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:466) 
    at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:497) 
    at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForList(SimpleJdbcTemplate.java:223) 
    at com.db.timexis.dao.UserDaoJdbc.getListOfAllUsers(UserDaoJdbc.java:137) 
    at com.db.timexis.service.AuthServiceImpl.getListOfAllUsers(AuthServiceImpl.java:77) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at $Proxy0.getListOfAllUsers(Unknown Source) 
    at com.mail.timexis.ReminderBean.execute(ReminderBean.java:25) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276) 
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260) 
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86) 
    at org.quartz.core.JobRunShell.run(JobRunShell.java:216) 
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) 
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.) 
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225) 
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880) 
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113) 
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) 
    ... 30 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) 
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
    at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38) 
    at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294) 
    at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247) 
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221) 
    ... 33 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The driver was unable to create a connection due to an inability to establish the client portion of a socket. 

This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable. 

For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system reconfiguration may also be required. 

For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271). 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) 
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122) 
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181) 
    ... 46 more 
Caused by: java.net.BindException: Address already in use: connect 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) 
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 
    at java.net.Socket.connect(Socket.java:519) 
    at java.net.Socket.connect(Socket.java:469) 
    at java.net.Socket.<init>(Socket.java:366) 
    at java.net.Socket.<init>(Socket.java:209) 
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) 
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:293) 
    ... 47 more 

代碼執行的每一分鐘:

public void execute() { 
     List<User> regularUsers = null; 
     try { 
      // get all regular users 
      ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "applicationContext.xml"); 
      AuthService authService = (AuthService) ctx.getBean("authService"); 

      regularUsers = authService.getListOfAllUsers(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     try { 
      if (regularUsers != null) { 
       for (User u : regularUsers) { 
        if (u.getEmail().matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)")) { 
         mailSender.sendMailReminder(u.getEmail(), 
           u.getFirstName()); 
         // log mail sending 
         Date date = Calendar.getInstance().getTime(); 
         SimpleDateFormat sdf = new SimpleDateFormat(
           "yyyy/MM/dd HH:mm:ss"); 
         String strDate = sdf.format(date); 
         System.out.println(strDate + "Reminder mail send..."); 
         // log mail sending 
        }else{ 
         System.out.println("invalid mail address"); 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

它執行的每一分鐘進行測試目的。它將在未來每週執行一次。

我DBCP propertyes:

jdbc.maxActive=15 
jdbc.maxIdle=10 
jdbc.minIdle=5 
jdbc.maxWait=5000 
jdbc.validationQuery=select 1 
jdbc.timeBetweenEvictionRunsMillis=1000 
jdbc.numTestsPerEvictionRun=10 
jdbc.minEvictableIdleTimeMillis=1200000 

的數據的基礎上是accessable槽命令行。

+0

有多少「普通用戶」? – SJuan76 2012-01-30 15:06:51

+0

我在我的DB中有兩個普通用戶 – 2012-01-30 15:08:47

回答

6

請注意,每次調用方法時都會創建一個新的應用程序上下文實例,並且不要關閉它。這意味着在此上下文中由bean分配的資源(例如,由連接池創建的連接)不會被釋放。

通常情況下,您需要在啓動應用程序時只創建一次應用程序上下文,並在關閉時關閉它,所以我建議您以這種方式實現它。

如果您在每次調用該方法時都需要創建新的應用程序上下文,請不要忘記在方法結束時(在finally block for reliability中)使用close()

另請注意,您應該爲連接池bean聲明合適的destroy-method,以便在上下文關閉時釋放資源。

+0

解決了,謝謝。 – 2012-01-30 15:32:31

1

主要的異常是「java.net.BindException:地址已經在使用:連接」。看起來你有連接池泄漏。你確定每個連接在使用後都關閉嗎?請檢查一下。

+0

我想這個問題與這個問題有很強的關聯,但是Spring必須關心連接。 – 2012-01-30 15:21:10

相關問題