Hibernate throws "Cannot do an operation on a closed statement" on MariaDB

If there is no DB request - select/update/delete for a few hours, it seems that either the session or the sessionFactory dies/goes to sleep.

If you read the stack trace, you’ll see that it’s not the SF that dies, but the database connection.

The SessionFactory and the Session are completely different things, so their lifecycles differ as well. The SessionFactory scope can be bound to the application while the Session lifecycle is usually bound to the incoming HTTP request thread.

If so - what is the way of changing/controlling it so that I don’t get this error again?

The problem does not come from Hibernate, it comes from the JDBC Driver or the database server:

Cannot do an operation on a closed statement

If you’re using connection pooling, set the maxLifetime property to a lower value than the one used by the JDBC driver or the database server.

Don’t expect a pooled connection to be kept alive for hours. Therefore, you need to address this connection pooling issue at the connection pool level.