我需要在我的spring hibernate应用程序中连接两个DB。
配置如下:
<?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>
我尝试在代码中获得两个不同的会话工厂,但它不起作用 我收到了这个错误:
25731 [23 Jun 2013 15:41:36 512] [qtp259913092-20] [ERROR] errorId=org.hibernate.HibernateException: No Session found for current thread
我尝试将我的课程定义如下:
@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();
}
}
但它不起作用 - 任何方向都会有所帮助!
感谢名单
----------------我试图将@Transactional添加到类中(如建议的那样)并获得此异常:
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]
答案 0 :(得分:2)
最后我就这样解决了:
@Repository
public class TstDao{
private static Logger LOG = LoggerFactory.getLogger(TstDao.class);
@Autowired
@Qualifier(value="transactionManager2")
private PlatformTransactionManager tm;
private TransactionTemplate transactionTemplate;
@PostConstruct
public void init(){
transactionTemplate = new TransactionTemplate(tm);
LOG.debug("tm="+tm);
}
@Autowired
@Qualifier(value="sessionFactory2")
SessionFactory sessionFactory;
public List<Obj_Test> getAllUsers(){
return transactionTemplate.execute(new TransactionCallback<List<Obj_Test>>() {
@Override
public List<Obj_Test> doInTransaction(TransactionStatus status) {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Obj_Test);
return crit.list();
}
});
}
}
在配置中我也复制了所有内容,它的工作原理就是这样。