-3

我的项目是 java ee Web 应用程序,我将休眠用于数据库目的。我现在将一个数据库连接到这个应用程序我需要将另一个 mysql 数据库连接到这个项目我该怎么做这可能吗?你能给我正确的链接或解决方案吗?

我为此使用了第一个答案thanx lot,但它给出了这个例外,你能帮我解决这个{16:02:39,552 WARN SettingsFactory - 无法获取连接元数据java.sql.SQLException:找不到适合hsqldb的驱动程序:hsql org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110) 的 java.sql.DriverManager.getConnection(Unknown Source) 的 java.sql.DriverManager.getConnection(Unknown Source) 的://127.0.0.1:9001/openreports ) 在 org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:72) 在 org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java: 1174) 在 org.efs.openreports.actions.admin.ServerDataAction.(ServerDataAction.java:23) 在太阳。reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org. springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:84) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:71) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor( ConstructorResolver.java:150) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:745) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。autowire(AbstractAutowireCapableBeanFactory.java:253) 在 com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:145) 在 com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:128) 在 com.opensymphony .xwork2.ObjectFactory.buildBean(ObjectFactory.java:143) 在 com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:113) 在 com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:275) 在 com。 opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:365) 在 com.opensymphony.xwork2.DefaultActionInvocation.access$000(DefaultActionInvocation.java:38) 在 com.opensymphony.xwork2.DefaultActionInvocation$1.doProfiling(DefaultActionInvocation.java:83)在 com.opensymphony.xwork2.util.profiling。UtilTimerStack.profile(UtilTimerStack.java:455) 在 com.opensymphony.xwork2.DefaultActionInvocation.(DefaultActionInvocation.java:74) 在 com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189) 在 org.apache.struts2。 impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41) 在 org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494) 在 org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter .java:125) 在 org.apache.catalina.core.ApplicationFilterChain。internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache .catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java :102) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache.coyote.http11 的 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)。 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java: 中的 Http11Processor.process(Http11Processor.java:859)602) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) 16:02:39,610 WARN SessionFactoryObjectFactory - InitialContext 没有实现 EventContext这是我的错误消息,这是什么原因,你能帮帮我吗…………}

4

1 回答 1

2

要连接两个数据库,您需要两个SessionFactory

提示

 SessionFactory sessionFactory1 = new  Configuration().configure("config1.cfg.xml").buildSessionFactory();
 SessionFactory sessionFactory2 = new Configuration().configure("config2.cfg.xml").buildSessionFactory();


void yourMethod() {
        try {
            session = sessionFactory1.openSession();
            // here play with session of sessionFactory1
            session.close();

            session = sessionFactory2.openSession();
           // here play with session of sessionFactory2
            session.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("It works");
        }
    }

如果您使用的是 spring,请参阅加载两个数据库

于 2012-10-26T09:27:15.780 回答