Miguel Gamboa
Miguel Gamboa

Reputation: 9353

DataSource properties

Why DataSource interface does not define the Standard Data Source Properties specified by the JDBC - DataSource?

With a reference dsof the DataSource type I cannot set the following properties like this:

public DbUtil(DataSource ds, String dbName, String port){
  ds.setServerName(dbName);
  ds.setPortNumber(port); 
}

Upvotes: 0

Views: 1140

Answers (3)

Mark Rotteveel
Mark Rotteveel

Reputation: 108983

The JDBC 4.1 specification, section 9.4.1 DataSource Properties answers your question:

DataSource properties are not intended to be directly accessible by JDBC clients. This design is reinforced by defining the access methods on the implementation class rather than on the public DataSource interface used by applications. Furthermore, the object that the client manipulates can be a wrapper that only implements the DataSource interface. The setter and getter methods for the properties need not be exposed to the client.

Also driver implementers can add setters/getters specific to their database

Upvotes: 1

mprabhat
mprabhat

Reputation: 20323

Because it doesn't make sense to set the database name or database configuration on a datasource manually, if that is allowed it will break the datasource functionality at runtime, while using Oracle you want to change the server details and you would start pointing to Sybase.

These parameters are supposed to be provided to the container and you can just use these during runtime, allowing a mechanism to override or change such details would be too dangerous.

Another thing to note here is that if these properties become part of the contract then it is just too much of information for the caller, which is against encapsulation.

Your code doesn't need to know about these details its part of configuration.

Upvotes: 1

adarshr
adarshr

Reputation: 62583

Because you're not supposed to pass in those values unlike plain old DriverManager. A DataSource is normally configured on the container and only looked-up using JNDI.

If you again start passing in these values, it defeats the whole point of having configurable DataSources.

Here is a quote from the API.

The DataSource interface is implemented by a driver vendor.

A DataSource object has properties that can be modified when necessary. For example, if the data source is moved to a different server, the property for the server can be changed. The benefit is that because the data source's properties can be changed, any code accessing that data source does not need to be changed.

Upvotes: 3

Related Questions