New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't propagate errors when using 'On Duplicate Key' - instead a 0/number is inserted? #309
Comments
|
That's very curious actually -- the I don't suppose you can check whether the equivalent JDBC statement triggers some bizarre error? Or maybe providing a test case? |
|
Good point. I'll try to see what JDBC does. Mainly it was only triggered by my own mistake (the typo), so I wanted to get the thing documented somewhere. |
|
And yeah, I stepped through the jdbi code, and for the life of me, couldn't figure out what code what have inserted it. |
|
I agree, I don't see how this is really possible. I spent a bit of time trying to figure out what I'd need to reproduce it but didn't come up with a compelling approach. Let us know as you figure out anything more or have a reproducer test case. |
|
And you are correct! I can post the entire program, but the basics should be enough: public class App
{
public static void jdbcTest() throws SQLException {
String user = "user";
String password = "pass";
String host = "localhost";
String db = "db";
try( Connection connection = DriverManager.getConnection("jdbc:mariadb://" + host + ":3306/" + db + "?user=" + user + "&password=" + password) ) {
try ( PreparedStatement st =
connection.prepareStatement("insert into test_jdbi_bug (id, data, counter) values (?,?,?) on duplicate key update counter = counter + ?") ) {
st.setInt(1, 1);
st.setString(2, "test");
st.setInt(3, 1);
st.setInt(4, 5);
st.execute();
}
}
}
public static void main( String[] args ) throws SQLException {
jdbcTest();
}
}With a schema like so: Ran it a few times, and then changed "counter" to "countered" and got: Server version was 5.5 (I think, I actually tried against a handy devint work server, not localhost), I can try against 10.x, but have a feeling this is in the driver anyways. |
|
Guess I should rename the test table though ;) |
|
Thanks for clearing this up! |
|
No problem - thanks for the quick tip on tracking it down :) Filed a bug against mariadb connectorj... in case the next person finds this issue via JDBI: |
I noticed and odd error. When using a @SqlUpdate with a "Insert into ... on duplicate key...", I had a column in the insert into that was incorrectly spelled, like so:
Instead of getting a SQL exception about a misnamed column, I was getting a random integer inserted into the SQL string, so I was getting an exception like:
"Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0 on duplicate key update col1 = col1 + :var1' at line 1
Query is : insert into some_table (col1, col2_with_typo) values ('some value', 1)0 on duplicate key update col1 = col1 +1'
Note the odd, spurious 0 after the parentheses - this is what actually happened - this somehow got inserted into the query. Sometimes it was a 1.
The odd part was after removing the "on duplicate key.." clause, I got an error on the lines like "column not found col2_with_typo".
I fixed the typo, and it worked fine.
It would be better to propagate the actual SQLException back up to the user, instead of inserting spurious text into the query.
The text was updated successfully, but these errors were encountered: