groovy
  1. groovy
  2. GROOVY-5375

Sql has newInstance methods which create connections but doesn't manage them

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-beta-3, 1.8.7
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      It would be useful to have variations of some of the groovy.sql.Sql#newInstance methods which took care of resource management, e.g. instead of this:

      def sql
      try {
        sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
        // sql operations here
      } finally {
        sql?.close()
      }
      

      You could do something like:

      Sql.withInstance(db.url, db.user, db.password, db.driver) { sql ->
        // sql operations here
      }
      

        Activity

        Hide
        Russel Winder added a comment -

        Isn't this something that should be handles using "try with resources" aka automated resource management (ARM) which came in new with Java 7?

        Show
        Russel Winder added a comment - Isn't this something that should be handles using "try with resources" aka automated resource management (ARM) which came in new with Java 7?
        Hide
        Paul King added a comment -

        We could certainly also add support for ARM in Groovy and make Sql implement AutoClosable; but in some sense, ARM is a poor man's substitute when you don't have "closures/blocks" and we'd probably want a solution for Java 6 anyway for those currently unable to move onto Java 7. So I still think something like "withInstance" would be useful and eventually we could also add ARM support, e.g. something like:

        try(Sql sql = Sql.newInstance(......)) {
          // sql operations here
        }
        

        Java would require the declaration to have a type implementing java.lang.AutoClosable. I presume Groovy would want a duck-typed weakened rule unless using static type checking - but we'd need to work through the finer points.

        Show
        Paul King added a comment - We could certainly also add support for ARM in Groovy and make Sql implement AutoClosable ; but in some sense, ARM is a poor man's substitute when you don't have "closures/blocks" and we'd probably want a solution for Java 6 anyway for those currently unable to move onto Java 7. So I still think something like "withInstance" would be useful and eventually we could also add ARM support, e.g. something like: try (Sql sql = Sql.newInstance(......)) { // sql operations here } Java would require the declaration to have a type implementing java.lang.AutoClosable . I presume Groovy would want a duck-typed weakened rule unless using static type checking - but we'd need to work through the finer points.
        Hide
        Paul King added a comment -

        withInstance variants added

        Show
        Paul King added a comment - withInstance variants added
        Hide
        Russel Winder added a comment -

        Seems to be working for me with master/HEAD 5f480e6f49585bab6efed3887dc3c2f9dbdd9b03

        Show
        Russel Winder added a comment - Seems to be working for me with master/HEAD 5f480e6f49585bab6efed3887dc3c2f9dbdd9b03
        Hide
        Russel Winder added a comment -

        Seems to work as anticipated.

        Show
        Russel Winder added a comment - Seems to work as anticipated.

          People

          • Assignee:
            Paul King
            Reporter:
            Paul King
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: