简体   繁体   English

如何优化 ActiveJDBC 中的连接管理?

[英]How to optimize connection managment in ActiveJDBC?

I am using ActiveJDBC as an alternative to Hibernate.我使用 ActiveJDBC 作为 Hibernate 的替代品。 I am using a Filter to start the connection whenever I run the application, but the connection will be there even when it is not needed by the application.每当我运行应用程序时,我都会使用过滤器来启动连接,但即使应用程序不需要连接,连接也会存在。 Also, when I try to run any query by findBySQL method, it also calls gets connection in the background.此外,当我尝试通过 findBySQL 方法运行任何查询时,它还会在后台调用获取连接。 Is there a way to optimize connections to the database?有没有办法优化与数据库的连接? There might be many queries at times, so opening and closing the connection every time a query is called might not be good for performance.有时可能会有很多查询,因此每次调用查询时打开和关闭连接可能对性能不利。 If there is any way or if I am missing some important point in understanding, do let me know.如果有任何方法或者我错过了一些重要的理解点,请告诉我。

You can use a fine-grained approach in the AppControllerConfig class.您可以在AppControllerConfig class 中使用细粒度方法。 Here is an example from a commercial project:这是一个商业项目的例子:

public class AppControllerConfig extends AbstractControllerConfig {

public void init(AppContext context) {
    add(new CatchAllFilter());
    add(new DefaultApiHeadersFilter());
    add(new DBConnectionFilter("default", true)).to(
            GroupsController.class,
            UsersController.class,
            SubjectsController.class,
            ChaptersController.class,
            LessonsController.class,
            LessonItemsController.class,
            JournalItemsController.class,
            GalleryItemsController.class
    );
  }
}

As you can see, you can apply a DBConnectionFilter to specific controllers.如您所见,您可以将DBConnectionFilter应用于特定的控制器。 The arguments in this example will tell DBConnectionFilter to automatically manage transactions for a default connection.此示例中的 arguments 将告诉DBConnectionFilter自动管理默认连接的事务。

Furthermore, you can specify exactly what actions require a DB connection like this:此外,您可以准确指定哪些操作需要数据库连接,如下所示:

add(new DBConnectionFilter("events", false)).to(FlowsController.class).forActions("report", "report_details", "webhook_test");

if you want the opposite, you can apply the filter to all actions, except some:如果您想要相反,您可以将过滤器应用于所有操作,除了一些:

  add(new DBConnectionFilter("default", true).to(TemplatesController.class).excludeActions("thumbnail");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM