trafodion icon indicating copy to clipboard operation
trafodion copied to clipboard

TRAFODION-2957 one stmt support multi-queries

Open mashengchen opened this issue 6 years ago • 41 comments

  1. reconstitute the constructor of TrafT4Statement & TrafT4PreparedStatement
  2. add new property allowMultiQueries to support exec multi-queries in one statement
  3. new class extends from TrafT4Statement & TrafT4PreparedStatement to exec multi-queries

mashengchen avatar Apr 23 '18 08:04 mashengchen

Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2582/

Traf-Jenkins avatar Apr 23 '18 08:04 Traf-Jenkins

Test Failed. https://jenkins.esgyn.com/job/Check-PR-master/2582/

Traf-Jenkins avatar Apr 23 '18 08:04 Traf-Jenkins

I think you should add license header into new java files.

moscowgentalman avatar Apr 23 '18 14:04 moscowgentalman

Can you explain a bit how this will work at the JDBC interface level? Will a user pass a statement like this to Connection::prepareStatement:

conn.prepareStatement("begin insert into t1 values(?,?); delete from t2; insert into t2 values (?); end");

If/when we resurrect the Trafodion code to support compound statements, will the interface work the same?

zellerh avatar Apr 24 '18 00:04 zellerh

I am not clear on the application interface to support this feature. Are we trying to conform to mysql way as given in this link https://stackoverflow.com/questions/10797794/multiple-queries-executed-in-java-in-single-statement

selvaganesang avatar Apr 24 '18 00:04 selvaganesang

@selvaganesang yes, the same feature

mashengchen avatar Apr 24 '18 01:04 mashengchen

@zellerh seems you lose a ";" after begin, if no ";" there will throw syntax error. so if query like "begin; insert into t1 values(?,?); delete from t2; insert into t2 values (?); end", actually there will have 5 preparedStatements , each do one query.

mashengchen avatar Apr 24 '18 01:04 mashengchen

New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2587/

Traf-Jenkins avatar Apr 24 '18 01:04 Traf-Jenkins

@mashengchen, I was thinking of a compound statement in my example. Your reply and the link that @selvaganesang provided helped me understand better. Trying to summarize what I heard, what you are implementing is a way to run multiple statements, in the following way:

  • Users need to specify an option in the connection string to enable this feature.
  • Users need to specify multiple statements, separated by semicolons (no begin/end block).
  • This feature does not implement a compound statement (begin end block).

If that's true, then that sounds ok to me. I just wanted to make sure we can add compound statements like in MySQL or IBM DB2 later without running into trouble with your feature.

zellerh avatar Apr 24 '18 01:04 zellerh

Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2587/

Traf-Jenkins avatar Apr 24 '18 04:04 Traf-Jenkins

@zellerh it's right in your summary, this PR is just a simple implement of the feature, the fully implement should change the sql engine code, which i am not familiar with it.

mashengchen avatar Apr 24 '18 05:04 mashengchen

You should ensure that all the methods of the standard PreparedStatement and Statement work seamlessly with the multi-queries statements. If a particular method doesn't make sense or can't work correctly, then it is better to return an error. For eg. addBatch, executeBatch etc.

In case of select statement, I couldn't figure out how result sets from different select statements are interfacing with the application.

The transaction boundaries needs to be explained too for both autocommit and non-autocommit mode.

selvaganesang avatar Apr 24 '18 14:04 selvaganesang

New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2610/

Traf-Jenkins avatar May 03 '18 08:05 Traf-Jenkins

Test Failed. https://jenkins.esgyn.com/job/Check-PR-master/2610/

Traf-Jenkins avatar May 03 '18 10:05 Traf-Jenkins

Previous Test Aborted. New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2611/

Traf-Jenkins avatar May 03 '18 10:05 Traf-Jenkins

Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2611/

Traf-Jenkins avatar May 03 '18 13:05 Traf-Jenkins

It looks like the weakReference of Statement object is removed. The weakReference is used to implicitly close the statement to clean up the resources associated with the Statement object if the application doesn't explicitly close the statement. This is to take care of resource leaks in the SQL side. Are there any other alternatives added to take care of such resource leaks. If not, I would suggest this PR may not be merged.

selvaganesang avatar May 03 '18 21:05 selvaganesang

@selvaganesang the weakReference still exist, I just refactor the construction of TrafT4Statement & TrafT4PreparedStatement, each time invoke TrafT4MultiQueriesStatement or TrafT4MultiQueriesPreparedStatement or TrafT4PreparedStatement, there will invoke the construction of TrafT4Statement, in which there will make the statement weakReference. see line 1415 of TrafT4Statement, it's just fold in the page.

mashengchen avatar May 04 '18 01:05 mashengchen

Thanks @mashengchen. Now I get it. You have refactored to call another constructor within a constructor. I remember there was one constructor of T4Statement (that is called internally) where the weakReference shouldn't be constructed. I am hoping that remains the same way even now.

selvaganesang avatar May 08 '18 15:05 selvaganesang

@selvaganesang i think what you descript is TrafT4Statement(); i did not change the behavior that. also i check all constructor, and there is no add behavior and no lose behavior.

mashengchen avatar May 10 '18 07:05 mashengchen

New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2638/

Traf-Jenkins avatar May 10 '18 07:05 Traf-Jenkins

Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2638/

Traf-Jenkins avatar May 10 '18 10:05 Traf-Jenkins

New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2643/

Traf-Jenkins avatar May 12 '18 06:05 Traf-Jenkins

Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2643/

Traf-Jenkins avatar May 12 '18 08:05 Traf-Jenkins

@selvaganesang and @zellerh, does this change look ready to merge now?

DaveBirdsall avatar May 14 '18 19:05 DaveBirdsall

All the comments I had are answered, so don't have any objections.

zellerh avatar May 15 '18 00:05 zellerh

I am ok with merging this PR but the following needs to be addressed soon.

  1. str.split(';') could split at the wrong place if there is ';' in string literal in the given sql string. This could result in unintended syntax error.
  2. Transaction semantics needs to be clarified.

I had raised these concerns earlier too

selvaganesang avatar May 15 '18 18:05 selvaganesang

I'm concerned that the issue Selva points out (incorrect parsing of semicolons) is not just a potential syntax error, but a potential security vulnerability -- via SQL injection into string values. If so, then "fix it later" is not good enough. It needs to be fixed before this is merged.

svarnau avatar May 15 '18 18:05 svarnau

Thanks @svarnau for pointing that out. I didn't sufficiently pick that up out of @selvaganesang's comments. I agree: the parsing issue needs to be fixed to avoid SQL injection vulnerabilities.

DaveBirdsall avatar May 15 '18 18:05 DaveBirdsall

Thanks, very good point! That always makes me think of this cartoon: https://xkcd.com/327/.

zellerh avatar May 15 '18 20:05 zellerh