gpt4 book ai didi

java - 从 Hibernate 和 Spring 连接到多个数据库

转载 作者:搜寻专家 更新时间:2023-11-01 03:24:51 26 4
gpt4 key购买 nike

我需要在我的 spring hibernate 应用程序中连接到两个数据库。

配置如下:

<?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>

我尝试在代码中获取两个不同的 session 工厂,但它不起作用我收到此错误:

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]

最佳答案

最后我是这样解决的:

@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();

}

});
}
}

在配置中,我也复制了所有内容,它就是这样工作的。

关于java - 从 Hibernate 和 Spring 连接到多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com