【问题标题】:Unable to create initial connections of pool Async Jersey+Spring+Tomcat无法创建池 Async Jersey+Spring+Tomcat 的初始连接
【发布时间】:2014-11-06 19:54:38
【问题描述】:

我有一个与 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"

【问题讨论】:

    标签: java mysql spring tomcat jdbc


    【解决方案1】:

    我解决了这个问题;我在我的 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
    

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 1970-01-01
      • 2018-10-23
      • 2017-02-02
      • 2019-06-19
      • 1970-01-01
      • 2018-08-08
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多