使用 Stripersist 在 Stripes Web 应用程序中拥有两个不同的数据库

发布于 2024-12-18 01:25:42 字数 173 浏览 3 评论 0原文

我将 Stripes 框架与 Stripersist 一起使用。

我希望我的 web 应用程序使用两个不同的数据库:在我的 persistence.xml 中,我想要两个不同的 persistence-unit

我怎样才能做到这一点?是否可以 ?

I use Stripes framework with Stripersist.

I want my webapp to use two different databases : in my persistence.xml, I want two different persistence-unit.

How can I do that ? Is it possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萧瑟寒风 2024-12-25 01:25:42

最后我们删除了 Stripersist 并构建了我们自己的堆栈。我们不尝试 Kdeveloper 提出的解决方案。

首先我们编写了一个 Stripe 拦截器:

@Intercepts({LifecycleStage.RequestInit, LifecycleStage.RequestComplete})
public class HibernateTxInterceptor implements net.sourceforge.stripes.controller.Interceptor {

private SessionFactory getUserSessionFactory(ExecutionContext context) {
  return (SessionFactory) context.getActionBeanContext().getServletContext().getAttribute(MyInitListener.CTX_SESSION_USERS);
}

private SessionFactory getDocumentSessionFactory(ExecutionContext context) {
  return (SessionFactory) context.getActionBeanContext().getServletContext().getAttribute(MyInitListener.CTX_SESSION_DOCUMENTS);
}


public Resolution intercept(ExecutionContext context) throws Exception {
  LifecycleStage stage = context.getLifecycleStage();
  if (stage == LifecycleStage.RequestInit) { 
    // Init transactions
    Transaction userTx = getUserSessionFactory(context).getCurrentSession().beginTransaction();
    Transaction documentTx = getDocumentSessionFactory(context).getCurrentSession().beginTransaction();
  } else if (stage.equals(LifecycleStage.RequestComplete)) {
    // Commit Transactions
    Transaction userTx = getUserSessionFactory(context).getCurrentSession().getTransaction();
    userTx.commit();
    ...
    // Idem for documentTx...

  }
}

然后我们创建了一个 servlet 监听器来使用 Spring 注入两个 Hibernate 会话工厂:

public class Reef4iInitListener implements ServletContextListener {
  public static final String CTX_SESSION_USERS = "user";
  public static final String CTX_SESSION_DOCUMENTS = "document";
  private ServletContext servletContext;

  public void contextInitialized(ServletContextEvent e) {
    servletContext = e.getServletContext();

    final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(e.getServletContext());

    SessionFactory usersSessionFactory = (SessionFactory) springContext.getBean("usersSessionFactory");

    ... // do the same with for document factory
  }

  public void contextDestroyed(ServletContextEvent e) {
    SessionFactory userTx = (SessionFactory) e.getServletContext().getAttribute(CTX_SESSION_USERS);
    userTx.close();
  .... // do the same for document factory
  }

Finally we removed Stripersist and build our own stack. We don't try the solution proposed by Kdeveloper.

First we wrote a Stripe interceptor :

@Intercepts({LifecycleStage.RequestInit, LifecycleStage.RequestComplete})
public class HibernateTxInterceptor implements net.sourceforge.stripes.controller.Interceptor {

private SessionFactory getUserSessionFactory(ExecutionContext context) {
  return (SessionFactory) context.getActionBeanContext().getServletContext().getAttribute(MyInitListener.CTX_SESSION_USERS);
}

private SessionFactory getDocumentSessionFactory(ExecutionContext context) {
  return (SessionFactory) context.getActionBeanContext().getServletContext().getAttribute(MyInitListener.CTX_SESSION_DOCUMENTS);
}


public Resolution intercept(ExecutionContext context) throws Exception {
  LifecycleStage stage = context.getLifecycleStage();
  if (stage == LifecycleStage.RequestInit) { 
    // Init transactions
    Transaction userTx = getUserSessionFactory(context).getCurrentSession().beginTransaction();
    Transaction documentTx = getDocumentSessionFactory(context).getCurrentSession().beginTransaction();
  } else if (stage.equals(LifecycleStage.RequestComplete)) {
    // Commit Transactions
    Transaction userTx = getUserSessionFactory(context).getCurrentSession().getTransaction();
    userTx.commit();
    ...
    // Idem for documentTx...

  }
}

And we created a servlet listener to inject the two Hibernate session factories using Spring :

public class Reef4iInitListener implements ServletContextListener {
  public static final String CTX_SESSION_USERS = "user";
  public static final String CTX_SESSION_DOCUMENTS = "document";
  private ServletContext servletContext;

  public void contextInitialized(ServletContextEvent e) {
    servletContext = e.getServletContext();

    final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(e.getServletContext());

    SessionFactory usersSessionFactory = (SessionFactory) springContext.getBean("usersSessionFactory");

    ... // do the same with for document factory
  }

  public void contextDestroyed(ServletContextEvent e) {
    SessionFactory userTx = (SessionFactory) e.getServletContext().getAttribute(CTX_SESSION_USERS);
    userTx.close();
  .... // do the same for document factory
  }
雨巷深深 2024-12-25 01:25:42

您可以将多个持久性单元与 Stripersist 结合使用。尽管您不能再使用 getEntityManager() 方法:

Stripersist.getEntityManager()

您需要使用以下方法之一:

Stripersist.getEntityManager("Your PU name")

或者:

Stripersist.getEntityManager(YourEntity.class)

You can use multiple peristence units with Stripersist. Although you can no longer use the getEntityManager() method:

Stripersist.getEntityManager()

You need to use one of these:

Stripersist.getEntityManager("Your PU name")

Or:

Stripersist.getEntityManager(YourEntity.class)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文