在游戏框架中的动态名称Database

发布于 2025-02-06 04:49:14 字数 635 浏览 3 评论 0原文

我正在运行一个Java Play Framework设置,我希望在其中根据客户打电话来拥有多个数据库。我有一个JWT设置,那里有一个房客ID。但是,我无法掌握有关此最佳练习的头脑。到目前为止,我有这样的代码:

public class JavaNamedDatabase {
  private Database db;
  private DatabaseExecutionContext executionContext;
  private static final Logger.ALogger LOGGER = Logger.of(JavaNamedDatabase.class);


  @Inject
  public JavaNamedDatabase(
      @NamedDatabase("xxx") Database db, DatabaseExecutionContext executionContext) {
    this.db = db;
    this.executionContext = executionContext;
  }

我想在哪里进行“ xxx”动态,具体取决于租户的要求。 是否可以传递此参数,还是我需要分开的类?

也许最好的解决方案是只要每个客户运行一个实例,并将@namedDatabase作为运行时配置参数?

I'm running a java play framework setup where I would like to have several databases depending on what customer is making the call. I have a jwt setup where there is a tenant id. However I can't get my head around what's best practise in Play regarding this. As for now I have this code:

public class JavaNamedDatabase {
  private Database db;
  private DatabaseExecutionContext executionContext;
  private static final Logger.ALogger LOGGER = Logger.of(JavaNamedDatabase.class);


  @Inject
  public JavaNamedDatabase(
      @NamedDatabase("xxx") Database db, DatabaseExecutionContext executionContext) {
    this.db = db;
    this.executionContext = executionContext;
  }

where I would like to make "xxx" dynamic depending on which tenant is making the request.
Is it possible to pass this parameter or do I need to have separate classes?

Or maybe the best solution is just to have one instance running per customer and have the @NamedDatabase as a runtime config parameter?

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

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

发布评论

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

评论(1

度的依靠╰つ 2025-02-13 04:49:14

我找到了DBAPI,那里有一个数据库。

public class JavaNamedDatabase {
  private DBApi dbApi;
  private DatabaseExecutionContext executionContext;
  private static final Logger.ALogger LOGGER = Logger.of(JavaNamedDatabase.class);


  @Inject
  public JavaNamedDatabase(
      DBApi dbApi, DatabaseExecutionContext executionContext) {
    this.dbApi = dbApi;
    this.executionContext = executionContext;
  }

  public CompletionStage<Integer> addGenreToPlayItem(Integer playItemId, String genre) {
    return CompletableFuture.supplyAsync(
        () ->
            dbApi.getDatabase("xxx").withConnection(...``` 

I found DBApi where there is a getter for Database.

public class JavaNamedDatabase {
  private DBApi dbApi;
  private DatabaseExecutionContext executionContext;
  private static final Logger.ALogger LOGGER = Logger.of(JavaNamedDatabase.class);


  @Inject
  public JavaNamedDatabase(
      DBApi dbApi, DatabaseExecutionContext executionContext) {
    this.dbApi = dbApi;
    this.executionContext = executionContext;
  }

  public CompletionStage<Integer> addGenreToPlayItem(Integer playItemId, String genre) {
    return CompletableFuture.supplyAsync(
        () ->
            dbApi.getDatabase("xxx").withConnection(...``` 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文