<< Back to previous view

[JDBC-95] Evaluate body in context of provided java.sql.Connection without closing it finally Created: 14/Apr/14  Updated: 14/Apr/14  Resolved: 14/Apr/14

Status: Resolved
Project: java.jdbc
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Esko Lahti Assignee: Sean Corfield
Resolution: Declined Votes: 0
Labels: None


There are situations where:

  • There is Java code calling Clojure functions that access database.
  • Java code has already opened java.sql.Connection instance that should be to access the database instead of opening new connection at the Clojure side.

The with-db-connection macro could be used for this with {:connection conn} db spec. However as it wraps the body inside a with-open macro the provided connection will be closed in the end.

Practically the following macro could fix the problem. However I don't know if the already existing with-db-connection macro should be extended instead. Also skipped the documentation as this is only an example:

(defmacro with-provided-conn
[binding & body]
`(let [~(first binding) (jdbc/add-connection {} ~(second binding))]

Comment by Sean Corfield [ 14/Apr/14 11:01 AM ]

with-db-connection is a recent addition - as a convenience for users who want to open and close a connection for a specific piece of code. The default mode of operation in java.jdbc wass for users to manage their own connection. Your macro removes useful information from the db-spec. A more useful approach would be: (jdbc/add-connection db-spec (jdbc/get-connection db-spec)) so that the db-spec is maintained in the "connected spec". However, that requires the db-spec be passed in and locks you into calling get-connection which not all code may want. What you're asking for is not provided for a reason: it isn't universally applicable to all java.jdbc users - therefore I do not think it belongs in the library.

Generated at Fri Apr 18 13:13:43 CDT 2014 using JIRA 4.4#649-r158309.