Ilkar
Ilkar

Reputation: 2167

hibernate, spring - transaction

I have a project in Spring, where I have 2 connections to the database. Two because one is for read-only connections and the other is for read-write connections.

My problem is, when I try to invoke method to read only, I get:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Now I don't know, how to exactly configure my connections.

Below I've inserted my SERVICE CLASS file. The problem is when I'm trying to invoke method with sessionFactoryr , when I'm using sessionFactory - everything is OK.

My configuration is:

hibernate-context.xml:

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

        <context:property-placeholder location="/WEB-INF/spring.properties" />

        <!-- Enable annotation style of managing transactions -->
        <tx:annotation-driven transaction-manager="transactionManager" />   
        <tx:annotation-driven transaction-manager="transactionManagerr" />  

        <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
        <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
        <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
        <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->

<!-- First Connection -->

        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                     p:dataSource-ref="dataSource"
                     p:configLocation="${hibernate.config}"
                     p:packagesToScan="com.esb.scs"/>

        <!-- Declare a datasource that has pooling capabilities-->   
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                    destroy-method="close"
                    p:driverClass="${app.jdbc.driverClassName}"
                    p:jdbcUrl="${app.jdbc.url}"
                    p:user="${app.jdbc.username}"
                    p:password="${app.jdbc.password}"
                    p:acquireIncrement="5"
                    p:idleConnectionTestPeriod="60"
                    p:maxPoolSize="100"
                    p:maxStatements="50"
                    p:minPoolSize="10" />

        <!-- Declare a transaction manager-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                    p:sessionFactory-ref="sessionFactory" />

<!-- Second connection (read only)  -->                     

        <bean id="sessionFactoryr" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                     p:dataSource-ref="dataSourcer"
                     p:configLocation="${hibernate.config}"
                     p:packagesToScan="com.esb.scs"/>

        <!-- Declare a datasource that has pooling capabilities-->   
        <bean id="dataSourcer" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                    destroy-method="close"
                    p:driverClass="${app.jdbc.driverClassName}"
                    p:jdbcUrl="${app.jdbc.url}"
                    p:user="${appr.jdbc.username}"
                    p:password="${appr.jdbc.password}"
                    p:acquireIncrement="5"
                    p:idleConnectionTestPeriod="60"
                    p:maxPoolSize="100"
                    p:maxStatements="50"
                    p:minPoolSize="10" />

        <!-- Declare a transaction manager-->
        <bean id="transactionManagerr" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                    p:sessionFactory-ref="sessionFactoryr" />



    </beans>

Service Class:

    package com.esb.scs.service;


import static java.lang.System.out;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.esb.scs.domain.User;
import com.esb.scs.domain.UserReference;

@Service("userService")
@Transactional
public class UserService {

    protected static Logger logger = Logger.getLogger("service");

//  @Resource(name="sessionFactoryr")
//  private SessionFactory sessionFactoryr;

    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;

    @Resource(name="sessionFactoryr")
    private SessionFactory sessionFactoryr;


    public void addUser(HttpServletRequest request){

        logger.info("test");

        Session session = sessionFactory.getCurrentSession();

        PasswordEncoder encoder = new Md5PasswordEncoder();
        String hashedPassword = encoder.encodePassword(request.getParameter("password"), null);

        String login = request.getParameter("login");
        String password = hashedPassword;
        //String role = request.getParameter("role");
        String role = "normal";
        String ip = request.getParameter("ip");
        int active = 1;

        String referenceAddress = request.getParameter("referer");
        String sex = request.getParameter("sex");
        int age = Integer.parseInt(request.getParameter("age"));
        String country = request.getParameter("country");
        String city = request.getParameter("city");
        String education = request.getParameter("education");
        String profession = request.getParameter("profession");
        String branch = request.getParameter("branch");


        Transaction transaction = session.beginTransaction();

        User user = new User();

        UserReference userReference = new UserReference();

        Date dateCreate = new Date();

        user.setEmail(login);
        user.setPassword(password);
        user.setRole(role);
        user.setIp(ip);
        user.setDateCreate(dateCreate);
        user.setLastLoginDate(dateCreate);
        user.setActive(active);
        user.setToken("");

        userReference.setReferenceAddress(referenceAddress);
        userReference.setSex(sex);
        userReference.setAge(age);
        userReference.setCountry(country);
        userReference.setCity(city);
        userReference.setEducation(education);
        userReference.setProfession(profession);
        userReference.setBranch(branch);
        userReference.setSite(referenceAddress);
        userReference.setUser(user);



        userReference.setUser(user);
        user.getUserReferences().add(userReference);

        session.save(user);
        session.save(userReference);

        transaction.commit();

    }

    public List<User> getUser(String name, String password){

        Session session = sessionFactoryr.getCurrentSession();

        String sqlQuery = "FROM User WHERE email='"+name+"' AND password = '" +password+ "'" ;

        Query query = session.createQuery(sqlQuery).setMaxResults(1);

        return query.list();

    }


    public void updateUUID(String uuid, String login, String password){

        Session session = sessionFactory.getCurrentSession();

        Transaction transaction = session.beginTransaction();

        String hql = "update User set token = :token where email = :login and password = :password";

        Query query = session.createQuery(hql);

        query.setString("token", uuid);
        query.setString("login", login);
        query.setString("password", password);

        query.executeUpdate();

        transaction.commit();

    }





}

In addition, I have

<filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

in my web.xml.

According to your advice, if I add @Transactional("transactionManagerr") to my methods - it doesn't change anything, I still get the error.

Upvotes: 0

Views: 1217

Answers (2)

NimChimpsky
NimChimpsky

Reputation: 47280

If you are using transactional annotations you do not need to manually create a transaction in your service class.

You have specifed the class is transactional this means each method will have its own transaction created by spring.

You can also specifiy transactional on each method and further specifiy read only as and when required.

If you are using two different datasources you also need to specify which transaction manager is to be used.

<bean id="transactionManagerr" class="org.springframework.orm.hibernate3.HibernateTransactionManager"                p:sessionFactory-ref="sessionFactoryr">
    <qualifier value="transactionManagerr"/>            
</bean>

And in yr service class you do this :

@Transactional(value = "transactionManagerr")

Also I would think about renaming everything, just appending "r" to everything is not really descriptive.

Upvotes: 0

Piotr Gwiazda
Piotr Gwiazda

Reputation: 12212

I think you have to specify transaction manager name in @Transactional (e.g. @Transactional("transactionManagerr")) annotation. Read Documentation and Jira

Upvotes: 3

Related Questions