我的项目是java ee web应用程序我将hibernate用于数据库目的。我现在将一个数据库连接到这个应用程序我需要将另一个mysql数据库连接到这个项目我该怎么办呢?你能给我正确的链接或解决方案吗?高级
我使用了第一个答案,但是它给了你这个例外,请你帮我解决这个问题 {16:02:39,552 WARN SettingsFactory - 无法获取连接元数据 java.sql.SQLException:没有为hsqldb找到合适的驱动程序:hsql://127.0.0.1:9001 / openreports at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) 在org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110) 在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) at sun.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) 在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:84) 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:71) 在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) at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在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) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:602) 在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489) 在java.lang.Thread.run(未知来源) 16:02:39,610 WARN SessionFactoryObjectFactory - InitialContext没有实现EventContext这是我的错误msg什么是共鸣这可以请你帮助我..........}
答案 0 :(得分: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");
}
}
如果您使用的是弹簧,请参阅load two database