Bug #1599 Connection.getCatalog() returns sometimes wrong value.
Submitted: 19 Oct 2003 11:20 Modified: 11 Nov 2009 2:35
Reporter: Teemu Kuulasmaa Email Updates:
Status: Won't fix Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.9-stable OS:Any (all)
Assigned to: Mark Matthews CPU Architecture:Any

[19 Oct 2003 11:20] Teemu Kuulasmaa
Description:
Connection.getCatalog() returns sometimes wrong value. 

The Connection class has private variable "database" that should be the same as the current database. Unfortunately, this is not always the case. The value of this variable will be returned if user use method Connection.getCatalog(). 

If current database is "DB0" ( Connection.setCatalog("DB0") ) and user executes query "USE DB2" getCatalog() will return erroneously "DB0". Right return value should be "DB2". This is because JDBC driver doesn't get the name of the current database from server but use it's own private "database" variable. 

Many methods in Connection class use the "database" variable or pass it as a parameter. Quering current database from a server will increase network trafic and decrease performance. Hopefully there is a way to solve this problem.

Teemu

How to repeat:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://salakka.puijo.org:3306/test/?user=root");

Statement stmt = conn.createStatement();

System.out.println("Current DB (Should be test): "+ conn.getCatalog());
conn.setCatalog("mysql");
System.out.println("Current DB (Should be mysql): "+ conn.getCatalog());
stmt.execute("USE test");
System.out.println("Current DB (Should be test): "+ conn.getCatalog());
[19 Oct 2003 13:43] Mark Matthews
It is best practice in JDBC to set 'session' level settings (catalog, transaction isolation, autocommit, etc) using methods that are exposed in the JDBC API (and in fact it seems to be 'strongly encouraged' in most of the literature that I have read). If you do not, you can end up with un-defined situations such as the one you speak of. Also, it 'insulates' you from changes in the database, (such as the day sometime in the future when MySQL will have support for schemas and catalogs, where 'hardcoded' 'USE ....' statements might have un-intended side-effects).

We will look into adding the ability to configure the driver to always read these values from the database, but unfortunately there will _always_ be a performance penalty for doing so.
[19 Oct 2003 22:16] Teemu Kuulasmaa
It would be good idea to have ability to configure the driver to always read catalog from the server. Admittedly this will decrease performance but sometimes this kind of fuctionality is more important than pure performance. 

I am looking forward future JDBC driver versions!
[19 Nov 2003 10:35] Mark Matthews
This is supposed to be clarified in JDBC-4.0.

The current consensus (from speaking with many JDBC vendors), is that if you mess with autocommit, transaction isolation levels, or catalogs without going through the proper methods in Connection, you are on your own (the workarounds for reading this data properly when needed begin to get very complex).

We're still trying to figure out a way to do this cleanly for Connector/J.