У меня есть проект в Spring, где у меня есть 2 подключения к базе данных. Два, потому что один для соединений только для чтения, а другой - для соединений чтения-записи.
Моя проблема в том, что когда я пытаюсь вызвать метод только для чтения, я получаю:
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
Теперь я не знаю, как именно настроить мои соединения.
Ниже я вставил свой файл SERVICE CLASS. Проблема в том, что я пытаюсь вызвать метод с помощью sessionFactoryr
, когда я использую sessionFactory
- все в порядке.
Моя конфигурация:
зимуют-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>
Класс обслуживания:
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();
}
}
Кроме того, у меня есть
<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>
в моем web.xml.
По вашему совету, если я добавлю @Transactional("transactionManagerr")
к своим методам - это ничего не изменит, я все равно получу ошибку.