Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate, spring - transaction
    text
    copied!<p>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.</p> <p>My problem is, when I try to invoke method to read only, I get:</p> <p><code>No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here</code></p> <p>Now I don't know, how to exactly configure my connections.</p> <p>Below I've inserted my SERVICE CLASS file. The problem is when I'm trying to invoke method with <code>sessionFactoryr</code> , when I'm using <code>sessionFactory</code> - everything is OK.</p> <p>My configuration is:</p> <p>hibernate-context.xml:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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 "&gt; &lt;context:property-placeholder location="/WEB-INF/spring.properties" /&gt; &lt;!-- Enable annotation style of managing transactions --&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;tx:annotation-driven transaction-manager="transactionManagerr" /&gt; &lt;!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --&gt; &lt;!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html --&gt; &lt;!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --&gt; &lt;!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --&gt; &lt;!-- First Connection --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="${hibernate.config}" p:packagesToScan="com.esb.scs"/&gt; &lt;!-- Declare a datasource that has pooling capabilities--&gt; &lt;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" /&gt; &lt;!-- Declare a transaction manager--&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /&gt; &lt;!-- Second connection (read only) --&gt; &lt;bean id="sessionFactoryr" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSourcer" p:configLocation="${hibernate.config}" p:packagesToScan="com.esb.scs"/&gt; &lt;!-- Declare a datasource that has pooling capabilities--&gt; &lt;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" /&gt; &lt;!-- Declare a transaction manager--&gt; &lt;bean id="transactionManagerr" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactoryr" /&gt; &lt;/beans&gt; </code></pre> <p>Service Class: </p> <pre><code> 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&lt;User&gt; 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(); } } </code></pre> <p>In addition, I have </p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>in my web.xml.</p> <p>According to your advice, if I add <code>@Transactional("transactionManagerr")</code> to my methods - it doesn't change anything, I still get the error.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload