Bug #43666 MySql java-connector unable to retrieve getExportedKeys when SQL_MODE=ANSI_QUOT
Submitted: 16 Mar 2009 8:21 Modified: 17 Mar 2009 19:05
Reporter: naveen upadhyay Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:mysql-java-connector-5.1.7bin.jar OS:Any
Assigned to: Jess Balint CPU Architecture:Any

[16 Mar 2009 8:21] naveen upadhyay
Description:
MySql  java-connector unable to retrieve ResultSet using metadata.getExportedKeys when SQL_MODE=ANSI_QUOTES is set

How to repeat:
Here is a smaller program that illustrates the issue.

  Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  String url="jdbc:mysql://localhost:3306/dbname";
  con = DriverManager.getConnection(url,user,pass);
  Statement st=con.createStatement();
			      
   DatabaseMetaData metadata;
   ResultSet rs ;
 //Run the following statement alternately in two test runs.
   st.execute("SET SQL_MODE=ANSI_QUOTES"); 
     metadata = con.getMetaData();
     rs = metadata.getExportedKeys(null,null,"usr");
     
      if(rs.next()){
    	  System.out.println("Results Found!!!!");
      }
      else{
    	  System.out.println("Nothing!!!!");
     }
		
---------------------------------------------------------------------------
without setting ANSI_QUOTES as SQL_MODE i can read data for child tables but otherwise not.	 
Without setting the SQL_MODE we get the output  >>>Results Found!!!!    
On setting >>>>>>>>>>> Nothing!!!!

Suggested fix:
The  metadata.getExportedKeys(), should be able to retrieve the requested Resultset.
[16 Mar 2009 9:22] Tonci Grgin
Test case for our test framework.

Attachment: TestBug43666.java (text/x-java), 3.31 KiB.

[16 Mar 2009 9:25] Tonci Grgin
Hi Naveen and thanks for your report.

Verified as described against latest c/J 5.1 sources with test case attached:
.Loading JDBC driver 'com.mysql.jdbc.Driver'
Done.

Done.

java.vm.version         : 1.5.0_17-b04
java.vm.vendor          : Sun Microsystems Inc.
java.runtime.version    : 1.5.0_17-b04
os                      : Windows Server 2008, 6.0, x86
sun.management.compiler : HotSpot Client Compiler
-------------------------------------------------
Connected to 5.1.31-log
F
Time: 0,411
There was 1 failure:
1) testBug43666(testsuite.simple.TestBug43666)junit.framework.AssertionFailedError: No rows returned with ANSI_QUOTES

Jess, make note of what happens when you use I__S!
[16 Mar 2009 16:59] naveen upadhyay
suggested fix:
As a workaround u can :
Unset the SQL_MODE while querying the database for exportedKeys and and then again set the mode just before returning.
The same thing i can also do in my application to make child tables work..
However at a generic level it might lead to such mechanism in various other metadata calls..so it wont be a proper solution!
[17 Mar 2009 19:05] Jess Balint
You are side-stepping the driver by setting ANSI_QUOTES this way. The proper way to set it for the session is in the connection string, using "sessionVariables=sql_mode='ANSI_QUOTES'".