db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <...@intermeta.de>
Subject Re: Modify Transaction.rollback to ignore a null connection [was Re:
Date Fri, 01 Aug 2003 15:21:30 GMT
John McNally <jmcnally@collab.net> writes:

ATM, passing null into the Transaction does

--- cut ---
        if (con == null)
        {
            throw new NullPointerException("Connection object was null. "
                    + "This could be due to a misconfiguration of the "
                    + "DataSourceFactory. Check the logs and Torque.properties "
                    + "to better determine the cause.");
        }
--- cut ---

Which is equally bad. By putting "if(con != null)" checks around all
calls to commit, you simply work around this problem. IMHO, the whole
"connection object was null, throw a NPE" business is a wrong way.

If getting a null connection object is bad, then let commit() throw a
meaningful exception.

If getting a null connection object is possible, then let commit() work
on a null con and not return an error.

If the behaviour should be decideable, add a safeCommit() method to
the Transaction and use this in your error cases. But adding a bunch
of "if (con != null)" statements just to hide the problem with the
commit method, is IMNSVHO a hack. 

>Sorry, you are going to need to point me to the documentation that shows
>why null checks are a bad idea or a hack.  Without them the code was
>hiding root cause of many errors.

Because you plaster over the real problem. If you need null checks, add

public static void safeCommit(Connection con) throws TorqueException
{
  if (con != null)
  {
    commit(con);
  }
}

to the Transaction and do _ONE_ check. And replace all the checks with
calls to safeCommit(Connection con). Document that safeCommit() is
intended to be used in error cases where the con object can be legally
null.

To quote "Elements of Java Style": "Write code once and only once.".

>You think modifying Transaction.rollback to ignore a null connection is
>a good one.  I don't see how that could be a good thing, at least now an

Not rollback. commit. 

	Regards
		Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

Java, perl, Solaris, Linux, xSP Consulting, Web Services 
freelance consultant -- Jakarta Turbine Development  -- hero for hire

"You are being far too rational for this discussion."  
       --- Scott Robert Ladd in <3F1874B0.6030507@coyotegulch.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message