Release connection from database

25 views
Skip to first unread message

jdevelop

unread,
Aug 9, 2012, 6:41:05 AM8/9/12
to Circumflex Public Q&A
Hello!

How do I release connection to database gracefully? In my unit tests I
start several threads, which are connected to the database. And it
looks like they are still connected to the database even in case if I
stop them. If I call ormConf.connectionProvider.close() - it seems to
explode the world and no connections could be acquired anymore.

Boris Okunskiy

unread,
Aug 9, 2012, 6:55:34 AM8/9/12
to circumfl...@googlegroups.com
Greetings,

Connections are closed by the corresponding method in Transaction class: https://github.com/inca/circumflex/blob/master/circumflex-orm/src/main/scala/transaction.scala#L45

The obvious reason why the connections are not actually closed is that we rely on C3P0 to do the connection pooling stuff by default. That's true: it keeps database connections open all the time to save on transport-level negotiation and socket connection establishing.

Well, the actual question is whether your unit tests establish proper transaction demarcation. In other words, how do you close transactions in unit tests? Normally, all database operations should be executed inside Circumflex Context:

  Context.executeInNew { ctx =>
    // do your work here
  }

ORM will close every transaction at the end of the context lifecycle. If you are working on web app, Circumflex Web Framework will handle the transaction demarcation transparently (the transaction-per-request pattern), so you do not have to write `Context.executeInNew` stuff in your routers. But in all other scenarios, well, you need to demarcate the transactions on your own.

Best regards,
Boris Okunskiy 
Reply all
Reply to author
Forward
0 new messages