Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2793

NullPointerException when using SQL.rollback()/commit() when DataSource is being used

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.6-beta-2
    • SQL processing
    • None

    Description

      When initialising an SQL object like this:

      def Context ctx = new InitialContext();
      def DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/WhatEver");
      def sql = new Sql(ds);

      if you try later on to issue a:
      sql.commit() or sql.rollback(), you will get a NullPointerException. Looking at the current trunk version(10767), this caused due to access to this.useConnection not being protected properly, as it is done in the close() method, for example.

      On a related note, one would think that it is kind of weird that the same object can behave so differently when initialised with a DataSource or a Connection. When using a DataSource, every command uses a different connection and the allocation/release of each connection is done transparently, so close() does nothing and one cannot, for example, use one transaction for several operations. On the other hand, if initialised with one Connection, you have to work with it as if it were a "connection object" as you can perform everything in the same transaction and it is your responsibility to release the connection/object properly.
      I understand the rationale for both cases, but I wonder if having them under the same object is not confusing and error prone.

      In any case, throwing a NPE is not nice and should be substituted by a NOP or a RuntimeException that warns the developer that those methods are useless when SQL is used with a DataSource.

      S!

      Attachments

        Activity

          People

            paulk Paul King
            greeneyed Daniel Lopez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: