Should a database connection stay open all the time or only be opened when needed?

前端 未结 6 1401
天命终不由人
天命终不由人 2020-11-28 11:39

I have a bukkit plugin (minecraft) that requires a connection to the database.

Should a database connection stay open all the time, or be opened and closed when nee

6条回答
  •  孤街浪徒
    2020-11-28 12:22

    The Connection should be opened only when required. If it is open before the actual need, it reduces one active connection from the connection pool..so it ultimately effects the users of the application.

    So,it is always a better practice to open connection only when required and closing it after completion of process.

    Always try puttting you connection close logic inside the finally block that will ensure that your connection will be closed,even if any exception occurs in the application

    finally
    {
    connection.close()
    }
    

提交回复
热议问题