0

I am using ActiveJDBC as an alternative to 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. 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.

1 Answers1

1

You can use a fine-grained approach in the 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. The arguments in this example will tell DBConnectionFilter to automatically manage transactions for a default connection.

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");
ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • there is no AppControllerConfig class in my project, so where do I need to put this code? – abhishek rawat Feb 17 '21 at 05:40
  • @abhishekrawat, just saw this. You can refer to many example projects. Here is one: https://github.com/javalite/javalite-examples/tree/master/activeweb-simple/src/main/java/app/config – ipolevoy Apr 07 '21 at 23:02