0

I'm getting exception: No Session found for current thread, when I want to connect with database via hibernate, my cfg files: web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <display-name>Here we blog web application</display-name>

    <servlet>
        <servlet-name>web-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>web-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-dispatcher-servlet.xml</param-value>
    </context-param>

</web-app>

dispatcher servlet:

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

    <mvc:annotation-driven />
    <context:component-scan base-package="com.lime"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.postgresql.jdbc.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:8080/come_to_blog_db"/>
        <property name="username" value="postgres"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="packagesToScan">
            <array>
                <value>com.lime</value>
            </array>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
            </value>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

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

</beans>

I tried one solution (adding @Transactional annotation in service layer on method connecting to hibernate,but now i'm getting 2 exceptions: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection, any solutions? -maybe the problem is related with this: Cannot load JDBC driver class 'com.postgresql.jdbc.Driver' -no one was able to help me, I have all neccesary dependencies, but don't know what's wrong, thanks

3 Answers 3

0

You cannot connect because the driver is not on the classpath. You can download from here: http://jdbc.postgresql.org/download.html.

It could solve your jdbc class loading problems.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't exactly understand what should i do, download the jar and could You please explain me how to add it into classpath? but i forgot to write, I'm using maven, i have right dependency, and if you look at this picture- screenshot.cz/6YJM4, you can see i have the jar in my external libraries(and in the .jar is the required Driver class)
Hm. Is it added to your pom.xml? In this case it should be on your classpath. What is the stacktrace?
yes i have right dependency, I'm sure, stack trace for first case:screenshot.cz/UT91N, and for second:screenshot.cz/AHH0Z, it's probably for that warning, because Driver is not loaded, im desperated :/
@CharlieHarper The classname of the JDBC driver is normally org.postgresql.Driver and not com.postgresql.jdbc.Driver. Verify it by looking into the jar with any zip tool.
I would like to see the startup log stacktrace, not the request's please
|
0

Make sure you have wired in the SessionFactory bean into your dao implementation class. One way of doing that is as follow:

@Resource
private SessionFactory mySessionFactory;

Comments

0

Let's start the following segment:

<property name="driverClassName" value="com.postgresql.jdbc.Driver"/>

I used "com.postgresql.jdbc.Driver" as well for driverClassName and got the same error as you are getting. The only difference is I gave the database connection details in a resource.xml file and used resource injection to initiate the database connectivity from Java files.

<Context>

  <Resource name="jdbc/test" 
            auth="Container" type="javax.sql.DataSource"
               maxActive="20" maxIdle="5" maxWait="10000"
               username="postgres" password="password" 
               driverClassName="org.postgresql.Driver"
               url="jdbc:postgresql://localhost:5432/test?allowPublicKeyRetrieval=true"/>
                       
</Context>

My issue got resolved and there are no issues with database connectivity after I have used driverClassName="org.postgresql.Driver" instead of driverClassName="com.postgresql.jdbc.Driver".

Try the below code instead:

<property name="driverClassName" value="org.postgresql.Driver"/> 

Clean the project once or multiple times before running with the new code. Once the changes get implemented, it starts working properly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.