简体   繁体   中英

MySQL server not giving connection after 10 connections with hibernate and spring

This is my scenario. I have implemented spring mvc for basic login logout. When I use 10 mysql connections, I am not able to login, because I am not getting a new connection from mysql.

I am using hibernate to get mysql connections.

Can someone please help me out? [This is the configuration][1]

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">

    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false" />
    <property name="username" value="root" />
</bean>

<!-- Hibernate session factory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>schema.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.c3p0.timeout">10</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">100</prop>
        </props>
    </property>
</bean>

<bean id="dao" class="com.tavant.DAO.TestDAO">  
    <property name="sessionFactory" ref="sessionFactory"></property>  
</bean>

Edited:

public User findByUserName(String userName) {
    List<User> users = new ArrayList<User>();

    users = getSessionFactory().openSession().createQuery("from User where userName=?")
            .setParameter(0, userName).getResultList();

    if (users.size() > 0) {
        return users.get(0);
    } else {
        return null;
    }
}`

Please try:

Session session = getSessionFactory().openSession();
users = session.createQuery("from User where userName=?").setParameter(0, userName).getResultList();

    if (users.size() > 0) {
        return users.get(0);
    } else {
        return null;
    }

session.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM