Home > database >  Could not open Hibernate Session for transaction; nested exception is Could not open connection
Could not open Hibernate Session for transaction; nested exception is Could not open connection

Time:05-24

enter image description here

I created an application with spring hibernate, but I always get this error. This is my first application with hibernate, I read some guides but I can not solve this problem. Where am I doing wrong?

This is the code of my application

4.0.0 com.gss css_pos_mvn 0.0.1-SNAPSHOT war CSS Point of Sale

    <properties>
        <jsf2.version>2.2.14</jsf2.version>
        <spring.version>4.3.10.RELEASE</spring.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
    </properties>


    <dependencies>
        <!-- JSF Dep. -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>prenter code hereimefaces</artifactId>
            <version>6.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${jsf2.version}</version>enter code here
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${jsf2.version}</version>
        </dependency>
        <!-- Spring Dep. -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring ORM support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc11 
            <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc11</artifactId> 
            <version>21.5.0.0</version> </dependency> -->

        
    <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc6 -->
<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.4</version>
</dependency>



    </dependencies>

    <build>
        <finalName>Clever-Software-Solutions-POS</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>strong text

Hibernate

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- MySQL database -->
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!-- show SQL in console -->
        <property name="hibernate.show_sql">true</property>
        <!-- readable format for SQL output in the console -->
        <property name="format_sql">true</property>
        
        
        
        <mapping package="com.css.pos.domain.*"/>
        <mapping />
        <mapping />
        
          
            
        
    </session-factory>
</hibernate-configuration>

Spring Configuration

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    <context:component-scan base-package="com.css.pos"></context:component-scan>
    <!-- support spring annotation -->
    <context:annotation-config />
    <!-- support annotation transaction -->
    <tx:annotation-driven transaction-manager="transactionManager" />
 
    <!-- declare datasource -->
        <bean id="dataSource"
        >
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl21"/>
        <property name="username" value="c##gss_pos" />
        <property name="password" value="sys" />
    </bean>
 
    <!--Hibernate session factory configuration -->
    <bean id="sessionFactory"
        >
        <property name="dataSource" ref="dataSource" />
        <!-- load hibernate configuration file -->
        <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
        <!-- where to find the ORM classes -->
        <property name="packagesToScan" value="com.roytuts.hibernate.model" />
    </bean>
 
    <!-- Transaction manager -->
    <bean id="transactionManager"
        >
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    
</beans>

CodePudding user response:

Based on your information, it need your check on your oracle jdbc connection first, especially for below part:

<!-- declare datasource -->
    <bean id="dataSource"
    >
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl21"/>
    <property name="username" value="c##gss_pos" />
    <property name="password" value="sys" />
</bean>

You need ensure you can connect to your Oracle DB via the localhost:1521:orcl21 with proper user and password.

CodePudding user response:

Can you also use the latest JDBC driver? You are using 11.2.0.4. Try using 21.5.0.0 or 19.14.0.0. Check out OTN page.

  • Related