Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Problems using Spring, c3p0 and hibernate - Too many connections

$
0
0
Hello,

I have created simple rest API, using spring 3.2 that simply calls a service to retrieve a simple string from a mysql database.

As part of the poc and that the web service will be used for high traffic I have bee doing some performance testing using apache bench.

However when I hit my web service with 4000 connections with a concurrency of 100 my app falls over.

[CODE]
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientC onnectionException: Data source rejected establishment of connection, message from server: "Too many connections"

Here is my datasource config

Code:


        <bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
       
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
        <property name="minPoolSize" value="${c3p0.minPoolSize}" />
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
        <property name="maxAdministrativeTaskTime" value="5" />
 </bean>

</beans>

Here is my persistence context

Code:

<context:component-scan base-package="uk.co.mycompany.persistence.dao.impl" />

        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                        <ref local="sessionFactory" />
                </property>
        </bean>
       
        <context:property-placeholder location="classpath:/properties/database.properties, file://${mycompany.override.config.dir}/database.properties" ignore-resource-not-found="true" />
       
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="mappingResources">
                        <list>
                                <value>hibernate/mappings/uk/co/mycompany/persistence/model/PerformanceTest.hbm.xml</value>
                        </list>
                </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                  <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                  <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                  <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                  <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
                  <prop key="hibernate.auto_close_session">${hibernate.use_sql_comments}</prop>
                 
            </props>
        </property>
</bean>

</beans>

My service context
Code:


        <context:component-scan base-package="uk.co.mycompany.persistence.service.impl" />
        <tx:annotation-driven />

</beans>

My Service

Code:


@Service
@Transactional
public class LoginService implements ILoginService{
       
        @Autowired
        private LoginDao loginDao;
       
        private static Logger logger = LoggerFactory.getLogger(LoginService.class);
       
        public LoginService() {
                logger.debug("**Instantiation of LogionService**");
        }

        @Override
        public List<PerformanceTest> loadPerformanceTestData() {
                return loginDao.loadPerformanceTestData();
        }
       
       
}

MY DAO

Code:

@Repository
public class LoginDao implements ILoginDao{
                       
                private SessionFactory sessionFactory;
               
                private Session currentSession() {
                        return this.sessionFactory.getCurrentSession();
                }

                @Autowired
                public LoginDao(SessionFactory sessionFactory) {
                        this.sessionFactory = sessionFactory;
                       
                }

                @Override
                public List<PerformanceTest> loadPerformanceTestData() {
                        List performanceTest = currentSession().createCriteria(PerformanceTest.class).list();       
                        return performanceTest;
                }
               
}


I was under the impression that spring manages the opening and closing of the connection? Can anyone help me understand what is wrong or point me in the right direction?

Viewing all articles
Browse latest Browse all 297

Trending Articles