无法创建池Async Jersey + Spring + Tomcat的初始连接

时间:2014-11-06 19:54:38

标签: java mysql spring tomcat jdbc

我有一个与Jersey + Spring异步的Web服务并使用Tomcat 7.

我在tomcat中设置了server.xml,如:

<Connector port="8080" address="localhost"     
 maxThreads="250" maxHttpHeaderSize="8192"
 emptySessionPath="true" protocol="HTTP/1.1"
 enableLookups="false" redirectPort="8443" acceptCount="100"
 connectionTimeout="20000" disableUploadTimeout="true" />

我在beam.xml中有这个:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ldap="http://www.springframework.org/schema/ldap"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/ldap 
        http://www.springframework.org/schema/ldap/spring-ldap.xsd">


    <context:property-placeholder location="/config/application.properties" />      

    <!-- Initialization for data source JDBC -->
    <bean id="dataSource"
        class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="${jdbc.initialSize}" />
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}" />
        <property name="minIdle" value="${jdbc.minIdle}" />
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
    </bean>

    <!-- Definition for studentJDBCTemplate bean -->
    <bean id="studentJDBCTemplate" class="com.me.database.StudentJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>

    <!-- Initialization for TransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    ...

在我的StudentJDBCTemplate中,我有:

import org.apache.tomcat.jdbc.pool.DataSource;  
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class StudentJDBCTemplate implements StudenteDAO {

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;
    private PlatformTransactionManager transactionManager;

    private int p_id;
    private int c_id;


    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }

    public void setTransactionManager(
            PlatformTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

我尝试使用带有3个http采样器请求的Jmeter和一个带有4个线程数的线程组,加速周期1和循环计数1.但是当我尝试执行此测试时出现此错误:

PM org.apache.tomcat.jdbc.pool.ConnectionPool init
GRAVE: Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"

1 个答案:

答案 0 :(得分:0)

我决心用这个;我在我的Tomcat配置上放了context.xml:

<Resource 
auth="Container" 
driverClassName="com.mysql.jdbc.Driver" 
maxActive="100" maxIdle="30" 
maxWait="10000" 
name="jdbc/UsersDB" 
password="" 
type="javax.sql.DataSource" 
url="jdbc:mysql://localhost:3306/mydb" 
username="root"/>

使用以下命令更改我的beam.xml配置:

<!-- Initialization for data source JDBC -->
    <bean id="dataSource"
        class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/UsersDB"/>
        <property name="lookupOnStartup" value="true"/>
        <property name="proxyInterface" value="javax.sql.DataSource"/>
    </bean>

添加Tomcat目录/ lib mysql-connector-java-VERSION-bin.jar和我的项目

aopalliance-1.0.jar
commons-dbcp2-2.0.1.jar 
commons-pool2-2.2.jar