I have this in my servlet:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="12345" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.tricas.models" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.default_schema">test</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
and my DaoImp
#Repository
#Transactional
public class UserDaoImp implements UserDao {
#Autowired
SessionFactory session;
public List<Users> list() {
return session.getCurrentSession().createQuery("from Users").list();
}
here is my HibernateUtil
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
And After executing the application I have a NullPointerException:
SEVERE [http-nio-8084-exec-97] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service () for servlet [spring-web] in context with path [/ Holaspringmvc] threw exception [Request processing failed; Nested exception is java.lang.NullPointerException] with root cause
Java.lang.NullPointerException
At com.tricas.dao.UserDaoImp.list (UserDaoImp.java:32)
Please help me.
Be simpler. Just declare SessionFactory bean
#Bean
public AbstractSessionFactoryBean sessionFactoryBean(){
AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
return sessionFactoryBean;
}
similar for LocalSessionFactoryBean
btw: did you define component-scan ?
<context:component-scan base-package="<my.base.package>" />
I found the error, results that I had to define in the service and in the controller also with #Autowired
Here is my Service.
#Autowired
UserDao usrdao;
//private UserDao usrdao = new UserDaoImp();
#Transactional
public List<Users> getAllUsers() {
return usrdao.list();
}
and here is my controller
#Autowired
UserService usrv;
//private UserService usrv = new UserService();
#RequestMapping(value = "/verusuarios", method = RequestMethod.GET)
public String listPersons(Model model) {
List<Users> list = usrv.getAllUsers();
model.addAttribute("user", new Users());
model.addAttribute("list", list);
return "verusuarios";
}
Additionally I must add to guide me from this answer: answer here
Related
I repurposed an existing hibernate-spring project and upgraded to Hibernate 4 and Spring 4 and added multiple datasources using multitenancy. The application starts fine, the datasources are being read in using the MultiTenantDataSourceLookup class. When setting the new tenant, the tenant is resolved but then I get the nullpointerexception on line 41 of MultiTenantConnectionProviderImpl(See comment for line). I'm also using GenericHibernateDAO if that helps. I can post that code by request. I'm new to spring so the problem may be a very simple one. However, if more code is needed to help me, I will be happy to share more as I troubleshoot and research the problem myself. Any help will be greatly appreciated. Thanks. Here is the full stack trace: http://pastebin.com/LjyhTwvY
MultiTenantConnectionProviderImpl.java
public class MultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl
{
#Autowired
private DataSource defaultDataSource;
#Autowired
private DataSourceLookup dataSourceLookup;
/**
* Select datasources in situations where not tenantId is used (e.g. startup processing).
*/
#Override
protected DataSource selectAnyDataSource() {
return defaultDataSource;
}
/**
* Obtains a DataSource based on tenantId
*/
#Override
protected DataSource selectDataSource(String tenantIdentifier) {
//Below is line 41 where the nullpointerexeption is occurring
DataSource ds = dataSourceLookup.getDataSource(tenantIdentifier);
return ds;
}
}
CurrentTenantIdentifierResolverImpl.java
public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {
private static final String KEY_TENANTID_SESSION = "hibernate.tenant_identifier_resolver";
private static final String DEFAULT_TENANTID = "customer1";
public String resolveCurrentTenantIdentifier() {
String tenant = resolveTenantByHttpSession();
System.out.println("Tenant resolved: " + tenant);
return tenant;
}
/**
* Get tenantId in the session attribute KEY_TENANTID_SESSION
* #return TenantId on KEY_TENANTID_SESSION
*/
public String resolveTenantByHttpSession()
{
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//If session attribute exists returns tenantId saved on the session
if(attr != null){
HttpSession session = attr.getRequest().getSession(false); // true == allow create
if(session != null){
String tenant = (String) session.getAttribute(KEY_TENANTID_SESSION);
if(tenant != null){
return tenant;
}
}
}
//otherwise return default tenant
return DEFAULT_TENANTID;
}
public boolean validateExistingCurrentSessions() {
return true;
}
}
Context.xml
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<bean id="multitenancyConnectionProvider"
class="com.github.elizabetht.util.MultiTenantConnectionProviderImpl"/>
<bean id="dataSourceLookup"
class="com.github.elizabetht.util.MultiTenantDataSourceLookup"/>
<bean id="tenantResolver"
class="com.github.elizabetht.util.CurrentTenantIdentifierResolverImpl"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.github.elizabetht.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.jdbc.lob.non_contextual_creation">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.multiTenancy">DATABASE</prop>
<prop key="hibernate.multi_tenant_connection_provider">com.github.elizabetht.util.MultiTenantConnectionProviderImpl</prop>
<prop key="hibernate.tenant_identifier_resolver">com.github.elizabetht.util.CurrentTenantIdentifierResolverImpl</prop>
</props>
</property>
</bean>
<bean id="defaultDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/studentEnrollment" />
<property name="username" value="springy" />
<property name="password" value="pass" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="autodetectDataSource" value="false"/>
</bean>
OUTPUT
Tenant resolved: customer1
Feb 25, 2017 1:34:31 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [studentHibernateServlet] in context with path [/StudentEnrollmentWithSpring] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.github.elizabetht.util.MultiTenantConnectionProviderImpl.selectDataSource(MultiTenantConnectionProviderImpl.java:41)
at org.hibernate.engine.jdbc.connections.spi.AbstractDataSourceBasedMultiTenantConnectionProviderImpl.getConnection(AbstractDataSourceBasedMultiTenantConnectionProviderImpl.java:52)
at org.hibernate.internal.AbstractSessionImpl$ContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:423)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
I changed my hibernate properties to the following and everything works now:
<property name="hibernateProperties">
<map>
<entry key="hibernate.multi_tenant_connection_provider" value-ref="multitenancyConnectionProvider"/>
<entry key="hibernate.tenant_identifier_resolver" value-ref="tenantResolver"/>
<entry key="hibernate.multiTenancy" value="DATABASE"/>
</map>
</property>
it's my first time I'm using hibernate so I've got a little question.
Is this way of using hibernate 5.1 with hikariCP 2.4.3 correct?
I'm not sure if the pooling is working like this.
should I call
Session session = sessionFactory.openSession();
or
Session session = sessionFactory.getCurrentSession();
to get a session?
when i call
session.close();
is the connection closed or given back to the pool.
the HibernateDataStore is used by threads for accessing the db
thanks for help
The Config
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.provider_class">
org.hibernate.hikaricp.internal.HikariCPConnectionProvider
</property>
<property name="hibernate.hikari.dataSourceClassName">
com.mysql.jdbc.jdbc2.optional.MysqlDataSource
</property>
<property name="hibernate.hikari.dataSource.url">
jdbc:mysql://localhost/xyz
</property>
<property name="hibernate.hikari.dataSource.user">user</property>
<property name="hibernate.hikari.dataSource.password">xyz</property>
<property name="hibernate.hikari.minimumIdle">20</property>
<property name="hibernate.hikari.maximumPoolSize">100</property>
<property name="hibernate.current_session_context_class">
org.hibernate.context.internal.ThreadLocalSessionContext
</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Names the annotated entity class -->
<mapping class="XYZ"/>
</session-factory>
</hibernate-configuration>
My DataStore-Class
public class HibernateDataStore implements DataStore {
private static final Logger logger = LogManager.getLogger();
/** factory for hibernate sessions */
private static SessionFactory sessionFactory;
static {
setUpHibernate();
}
/**
* set up the hibernate session-factory
*/
private static void setUpHibernate() {
logger.debug("building session factory");
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure( "config/server/hibernate.cfg.xml" )
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
logger.error("problems building hibernate sessionFactory " + e.getMessage());
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
logger.debug("finished building session factory");
}
#Override
public void storeProduct(Product product) {
logger.info("storing product: " + product.getTitle());
Instant start = Instant.now();
final Session session = sessionFactory.openSession();
//final Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save( product );
session.getTransaction().commit();
session.close();
Instant stop = Instant.now();
logger.debug("product saved (took " + Duration.between(start, stop).toMillis() + "ms)");
}
}
I think you should use Spring to handle session factory
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="dataSourceProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/xyz</prop>
<prop key="user">root</prop>
<prop key="password"></prop>
</props>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.xyz.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hikari.dataSource.minimumIdle">5</prop>
<prop key="hibernate.hikari.dataSource.maximumPoolSize">20</prop>
<prop key="hibernate.hikari.dataSource.idleTimeout">30000</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
I'm a newbie in Spring and Hibernate and I'm having a NPE in this line:
Session session = getSessionFactory().getCurrentSession();
This is my UserDAO class
public class UserDAO implements IUserDAO{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Override
public Integer updateUser(User user) {
// TODO Auto-generated method stub
return null;
}
#Override
public User searchUserIfExists(String username, String password) {
Session session = getSessionFactory().getCurrentSession();
Query query = session.createQuery(SEARCH_USER_ACCT);
query.setParameter("username", username);
query.setParameter("password",password);
List<User> userList = query.list();
System.out.println(userList.size());
User user = null;
return user;
}
And this is my UserController
#SessionAttributes(value = {"userSession"})
#Controller
public class UserController {
private IUserDAO userDAO;
#Autowired
public UserController(IUserDAO userDAO) {
this.userDAO = userDAO;
}
#RequestMapping(value = "/processLogin.htm", method=RequestMethod.POST)
public String login(#ModelAttribute User user,#RequestParam("username") String username,
#RequestParam("password") String password,
HttpServletRequest request, HttpServletResponse response){
userDAO = new UserDAO();
User userOb = userDAO.searchUserIfExists(username, password);
if(userOb != null){
ModelAndView modelAndView = new ModelAndView();
String role = userOb.getUserRole();
switch(role){
case "admin":
modelAndView.addObject("userSession", userOb);
modelAndView.addObject("userId", userOb.getUserId());
modelAndView.setViewName("/adminPage_admin");
break;
case "customer":
modelAndView.addObject("userSession", userOb);
modelAndView.addObject("userId", userOb.getUserId());
modelAndView.setViewName("/customerpage_customer");
break;
}
}
return "";
}
}
This is my xml configuration for spring
<?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:beans="http://www.springframework.org/schema/beans"
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">
<!-- Enable #Controller annotation support -->
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<!-- Map simple view name such as "test" into /WEB-INF/test.jsp -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/jsp" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Scan classpath for annotations (eg: #Service, #Repository etc) -->
<context:component-scan base-package="com.nacv2.*"/>
<context:spring-configured/>
<!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
username root and blank password. Change below if it's not the case -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_nac"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>com.nacv2.model.*</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
</props>
</property>
</bean>
<bean id="userDAO" class="com.nacv2.dataaccess.UserDAO">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Your problem is actually on this line:
userDAO = new UserDAO();
You have correctly wired Spring and Hibernate, but then you go and create a new object outside their knowledge, which won't know about the injected dependencies. Just use the one that's already been autowired for you.
I am using spring 3.2 with hibernate 4. I want to use spring to control the transactions.
However with the configuration mentioned below I get the
'Servlet.service() for servlet spring threw exception: org.hibernate.HibernateException: No Session found for current thread'
exception:
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* com.locator.service.impl.ServiceTypeService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="hbTransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- Hibernate session factory -->
<bean id="hbSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>../spring/model/ServiceType.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hbTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hbSessionFactory" />
</bean>
<bean id="serviceTypeService" class="com.locator.service.impl.ServiceTypeService">
<property name="serviceTypeDao" ref="serviceTypeDao"></property>
</bean>
<bean id="serviceTypeDao" class="com.locator.dao.impl.ServiceTypeDao">
<property name="sessionFactory" ref="hbSessionFactory"></property>
</bean>
The code for the Dao layer and the Service is as follows:
public class ServiceTypeDao implements IServiceTypeDao{
private static final Log log = LogFactory.getLog(ServiceTypeDao.class);
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory(){
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public ServiceType findById(int id) {
log.debug("getting ServiceType instance with id: " + id);
try {
Session session = getSessionFactory().getCurrentSession();
ServiceType instance = (ServiceType) session.get("com.locator.model.ServiceType", id);
if (instance == null) {
log.debug("get successful, no instance found");
} else {
log.debug("get successful, instance found");
}
instance.setName(instance.getName()+"0");
session.saveOrUpdate(instance);
return instance;
}catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
}
public class ServiceTypeService implements IServiceTypeService{
private ServiceTypeDao serviceTypeDao;
public void setServiceTypeDao(ServiceTypeDao serviceTypeDao){
this.serviceTypeDao = serviceTypeDao;
}
public ServiceType getServiceTypeById(int id){
return serviceTypeDao.findById(id);
}
}
Replacing getSessionFactory().getCurrentSession() with getSessionFactory().openSession() will resolve the above issue however, it will mean that the developer will then be responsible for the session open/close rather than spring. Therefore, please advise how this can be resolved using spring.
I was able to resolve the issue. It was occurring due to the following problems:
The Service class had not been Auto wired into the controller i.e. the #Autowired annotation was missing.
The configuration for the web.xml had to be modified with the listener class 'org.springframework.web.context.ContextLoaderListener' and the context-param was added.
I have an Spring+Hibernate/Flex application that needs to switch dynamically between database schemas. To accomplish that I implemented a AbstractRoutingDataSource following this article. Unfortunately to change between dataSource's doesn't work.
Someone could help me?
I followed this link: Spring + Hibernate SessionFactory + AbstractRoutingDataSource
My code is:
- ApplicationContext:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url2}</value>
</property>
<property name="username">
<value>${jdbc.username2}</value>
</property>
<property name="password">
<value>${jdbc.password2}</value>
</property>
</bean>
<bean id="routingDS" class="br.com.cpb.gtf.infra.RoutingDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="br.com.cpb.gtf.infra.SchemaConstants.TESTE" value-ref="dataSource2"/>
<entry key="br.com.cpb.gtf.infra.SchemaConstants.PRODUCAO" value-ref="dataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource2"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.cpb.*" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.query.substitutions">true '1', false '0'" </prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
-RoutingDataSource:
package br.com.cpb.gtf.infra;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class RoutingDataSource extends AbstractRoutingDataSource {
#Override
protected Object determineCurrentLookupKey()
{
return Globals.getSchema();
}
}
-SchemaConstants
package br.com.cpb.gtf.infra;
public class SchemaConstants {
public static final String PRODUCAO = "dataSource";
public static final String TESTE = "dataSource2";
}
-Globals:
package br.com.cpb.gtf.infra;
public class Globals {
private static final ThreadLocal<String> schemaHolder = new ThreadLocal<String>();
public static void setSchema(String schema) {
schemaHolder.set(schema);
}
public static String getSchema() {
return schemaHolder.get();
}
public static void clearCustomerType() {
schemaHolder.remove();
}
}
-Service:
#RemotingInclude
#Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public Adm_Usuario logar(Adm_Usuario user) {
Globals.clearCustomerType();
Globals.setSchema(SchemaConstants.TESTE);
Adm_UsuarioDao dao = new Adm_UsuarioDao(sessionFactory);
Adm_Usuario admUsuario = dao.logar(user);
setLoggedUserOnSession(admUsuario);
return admUsuario;
}
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
in property dataSource, ref should be routingDS:
<property name="dataSource" ref="routingDS" />