org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection

I am new to Spring and creating login page using Spring mvc, I cannot connect database(mysql) to the application.

I had the latest version of hibernate 5. the exception is:

Type Exception Report Message Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection Descr

  <?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="com.vikram"></context:component-scan>

<!-- database connection pooling  -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/webapplication?useSSL=false" />
    <property name="user" value="root" />
    <property name="password" value="vikram#123" />

    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="30000" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.telusko.tusk.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
</bean>

<bean id="myTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="myTransactionManager" /> 

</beans>


enter code here
package com.vikram.webapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vikram.dao.Logindao;
import com.vikram.entity.Loginentity;

@Controller
public class IndexController 

{
    @Autowired
    Logindao logindao;

    Loginentity loginet;

    @RequestMapping("/")
    public String home()
    {
        return "index.jsp";
    }

    @RequestMapping("/login.htm")
    public String login()
    {
        System.out.println("I am at login");
        return "login.jsp";
    }

    @RequestMapping("/checklogin")
    public String collectingloginvalues(@ModelAttribute ("loginet") Loginentity loginet)
    {
        System.out.println("I am at checkinlogin");
        logindao.checklogin(loginet);
        return "index.jsp";
    }

}

1 Answers

0
Jeeva D On

As you have not provided the full stack trace, I could suggest you the below solution. If this has occurred because of connection pool problem , then please click here! for the answer. Try this out it will work.