Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values
От | Kevin Grittner |
---|---|
Тема | Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values |
Дата | |
Msg-id | 1359403348.20803.YahooMailNeo@web162902.mail.bf1.yahoo.com обсуждение исходный текст |
Ответ на | Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values (Nathaniel Waisbrot <waisbrot@highfleet.com>) |
Список | pgsql-jdbc |
Nathaniel Waisbrot <waisbrot@highfleet.com> wrote: > I found this while working with another JDBC driver (Stels XML > driver). The Postgresql driver's connect() method is expecting > the passed Properties object to have only string values, but this > is not actually guaranteed to be the case. In addition to Dave's perfectly valid point about DriverPropertyInfo, please note the following about the Properties class: http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html This page includes: | Each key and its corresponding value in the property list is a | string. and: | Because Properties inherits from Hashtable, the put and putAll | methods can be applied to a Properties object. Their use is | strongly discouraged as they allow the caller to insert entries | whose keys or values are not Strings. The setProperty method | should be used instead. If the store or save method is called on | a "compromised" Properties object that contains a non-String key | or value, the call will fail. Similarly, the call to the | propertyNames or list method will fail if it is called on a | "compromised" Properties object that contains a non-String key. So, while it is true that the Properties object is not guaranteed to contain only String objects, doing otherwise is "strongly discouraged", a Properties object containing other objects is considered "compromised", and some methods which are part of the Properties class will fail. I think it is best for a JDBC driver to throw an exception when it encounters such compromised objects. > I think that the PG driver should wrap the NullPointerException > in a SQLException, which would allow the DriverManager to attempt > to use other JDBC drivers to make the connection. Well, it should not normally be the properties which determine which driver can be used for the connection, but the front of the URL. If it is the wrong driver, it should not throw any exception at all; it should return NULL: http://docs.oracle.com/javase/1.3/docs/api/java/sql/Driver.html#connect%28java.lang.String,%20java.util.Properties%29 | The driver should return "null" if it realizes it is the wrong | kind of driver to connect to the given URL. This will be common, | as when the JDBC driver manager is asked to connect to a given | URL it passes the URL to each loaded driver in turn. | | The driver should raise a SQLException if it is the right driver | to connect to the given URL, but has trouble connecting to the | database. But I do agree that it is bad form to allow a RuntimeException to be thrown by a driver method rather than catching that and wrapping it in a SQLException or returning null. -Kevin
В списке pgsql-jdbc по дате отправления: