Eine Verbindung zu mehreren DB von Hibernate und Spring

Ich muss eine Verbindung mit zwei DB in meinem spring-hibernate-Anwendung.

Die Konfiguration ist wie folgt:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.1.xsd
       http://www.springframework.org/schema/jms 
       http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">

<context:property-placeholder location="classpath:eloan.properties" />

<context:annotation-config />

<context:component-scan base-package="com.eloan" />

<tx:annotation-driven transaction-manager="transactionManager"
    proxy-target-class="true" />


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">

    <property name="properties">
        <props>
            <prop key="useUnicode">true</prop>
            <prop key="characterEncoding">UTF-8</prop>
        </props>
    </property>

    <property name="driverClass" value="${jdbc.driver.classname}" />
    <property name="jdbcUrl" value="${jdbc.url}" />

    <property name="initialPoolSize" value="${c3p0.connections.min}" />
    <property name="minPoolSize" value="${c3p0.connections.min}" />
    <property name="maxPoolSize" value="${c3p0.connections.max}" />

    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

</bean>


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.eloan" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
        </props>
    </property>
</bean>

<!-- Declare a transaction manager -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- a test to connect to a second DB -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">

    <property name="properties">
        <props>
            <prop key="useUnicode">true</prop>
            <prop key="characterEncoding">UTF-8</prop>
        </props>
    </property>

    <property name="driverClass" value="${jdbc.driver.classname}" />
    <property name="jdbcUrl" value="${jdbc.url2}" />

    <property name="initialPoolSize" value="${c3p0.connections.min}" />
    <property name="minPoolSize" value="${c3p0.connections.min}" />
    <property name="maxPoolSize" value="${c3p0.connections.max}" />

    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

</bean>

<bean id="sessionFactory2"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource2" />
    <property name="packagesToScan" value="com.eloan" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
        </props>
    </property>
</bean>

Ich versuche, zwei verschiedene session-Fabriken in den code, aber es funktioniert nicht
Ich bin immer diese Fehlermeldung:

25731  [23 Jun 2013 15:41:36 512] [qtp259913092-20] [ERROR] errorId=org.hibernate.HibernateException: No Session found for current thread 

Ich versucht zu definieren, meine Klasse wie folgt:

@Repository
public class UsersBMDao extends HibernateTemplate{
    private static Logger LOG = LoggerFactory.getLogger(UsersBMDao.class);

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    public UsersBMDao(@Qualifier("sessionFactory2") SessionFactory sessionFactory) {
        super(sessionFactory);  
    }

    public List<UsersBM> getAllUsers(){
        Criteria crit = sessionFactory.getCurrentSession().createCriteria(UsersBM.class);
        return crit.list();
    }
}

Sondern seine nicht funktioniert - jede Richtung wird helfen!

Danke

---------------- Ich habe versucht, fügen Sie @Transactional an die Klasse (wie vorgeschlagen) und immer diese exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eloanDataController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.eloan.service.LoanService com.eloan.controller.EloanDataController.loanService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loanService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.eloan.service.SapLoanDetailsService com.eloan.service.LoanService.sldService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sapLoanDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.eloan.service.dao.UsersBMDao com.eloan.service.SapLoanDetailsService.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersBMDao' defined in file [/Users/nir/workspace/eloan/target/classes/com/eloan/service/dao/UsersBMDao.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.eloan.service.dao.UsersBMDao]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at javax.servlet.GenericServlet.init(GenericServlet.java:244) [javax.servlet-3.0.0.v201112011016.jar:na]
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:517) [jetty-servlet-8.1.7.v20120910.jar:8.1.7.v20120910]
InformationsquelleAutor Nir Sagiv | 2013-06-23
Schreibe einen Kommentar