1
有没有什么办法可以在多个JSP页面上共享EmbeddedGraphDatabase实例? 问题是,一旦启动tomcat服务器并为特定的Neo4j DB实例化EmbeddedGraphDatabase,如果您尝试实例化另一个graphDB,数据库将保持锁定状态。如何跨多个JSP页面共享EmbeddedGraphDatabase实例?
有没有什么办法可以在多个JSP页面上共享EmbeddedGraphDatabase实例? 问题是,一旦启动tomcat服务器并为特定的Neo4j DB实例化EmbeddedGraphDatabase,如果您尝试实例化另一个graphDB,数据库将保持锁定状态。如何跨多个JSP页面共享EmbeddedGraphDatabase实例?
据我所知,EmbeddedGraphDatabase
是你的对象,你希望它的实例在应用程序的多个JSP页面中共享。
您需要将它的实例放到应用程序范围中,并且它将对所有应用程序的类和JSP页面可见。
要做到这一点,你需要实现ServletContextListener
接口:
public class YourContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
EmbeddedGraphDatabase db = new EmbeddedGraphDatabase();
event.getServletContext().setAttribute("yourAttrName", db);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
event.getServletContext().removeAttribute("yourAttrName");
}
}
将其定义在web.xml
<listener>
<listener-class>your.package.YourContextListener</listener-class>
</listener>
而要得到它使用:
EmbeddedGraphDatabase db = getServletContext().getAttribute("yourAttrName");
希望这有助于。