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.
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");