Skip to content
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

execute stored procedure -> NPE with flag "debug = true" #420

Closed
moh-sushi opened this issue Aug 1, 2017 · 6 comments
Closed

execute stored procedure -> NPE with flag "debug = true" #420

moh-sushi opened this issue Aug 1, 2017 · 6 comments
Assignees

Comments

@moh-sushi
Copy link
Member

moh-sushi commented Aug 1, 2017

There is a NullPointerException for the case "execute stored procedure with debug = true".
Executing stored procedure was added with issue #389 .

Used jodd version : 3.8.6

change for test class 'CallableTest'

public class CallableTest extends DbBaseTest {

	@Test
	public void testCallableStatementDebugFalse() {
		DbBaseTest.DbAccess db = new PostgreSql();
		init();
		db.initDb();
		connect();

		db.createTables();
		try {
			test(false);
		} finally {
			db.close();
		}
	}

	@Test
	public void testCallableStatementDebugTrue() {
		DbBaseTest.DbAccess db = new PostgreSql();
		init();
		db.initDb();
		connect();

		db.createTables();
		try {
			test(true);
		} finally {
			db.close();
		}
	}

	private void test(final boolean debug) {
		DbSession session = new DbSession();

		DbQuery dbQuery = new DbQuery(session, "{ :upp = call upper( :str ) }");
                dbQuery.setDebug(debug);

		dbQuery.setString("str", "some lowercase value");
		dbQuery.outString("upp");

		DbCallResult r = dbQuery.executeCall();

		assertEquals("SOME LOWERCASE VALUE", r.getString("upp"));

		session.closeSession();
	}
}

A NPE is thrown at class 'LoggablePreparedStatementFactory' in method 'getQueryString'.
The class var getQueryStringMethod is null...

Workaround
I have subclassed jodd.db.DbQuery ...

    private static class DbQuery extends jodd.db.DbQuery {

        private DbQuery(Connection conn, String sqlString) {
            super(conn, sqlString);
        }

        private DbQuery(DbSession session, String sqlString) {
            super(session, sqlString);
        }

        private DbQuery(String sqlString) {
            super(sqlString);
        }

        @Override
        public String getQueryString() {
            if (callableStatement != null) {
                return sqlString;
            } else {
                return super.getQueryString();
            }
        }
    }

my thoughts for a fix:

  • call of LoggablePreparedStatementFactory.create within DbQueryBase#initializeJdbc:
		if (query.callable) {
			try {
				if (holdability != DEFAULT_HOLDABILITY) {
					callableStatement = connection.prepareCall(query.sql, type, concurrencyType, holdability);
				}
				else {
					callableStatement = connection.prepareCall(query.sql, type, concurrencyType);
				}
			}
			catch (SQLException sex) {
				throw new DbSqlException(this, "Error creating callable statement", sex);
			}

			preparedStatement = callableStatement;
			statement = callableStatement;

			return;
		}
  • extension of DbQueryBase#getQueryString :
	 */
	public String getQueryString() {
            if ((preparedStatement != null) && (!(preparedStatement instanceof CallableStatement)) && debug) {
                return LoggablePreparedStatementFactory.getQueryString(preparedStatement);
            }
		if (query != null) {
			return query.sql;
		}
		return sqlString;
	}

Greetz,
Sascha

@igr igr self-assigned this Aug 1, 2017
@igr
Copy link
Member

igr commented Aug 10, 2017

Hey Sascha!

Sorry for long response, I am trying to reproduce... which version of JDBC are you using?

@igr
Copy link
Member

igr commented Aug 10, 2017

Hey Sascha!

Ignore my message :) Reproduced :)

@igr
Copy link
Member

igr commented Aug 11, 2017

Working on this... need just a bit time, some bytecode issues :)

@moh-sushi
Copy link
Member Author

It sounds that you are working on a fix with calls to class LoggablePreparedStatementFactory :-)

My workaround (see above) is sufficient for now

@igr
Copy link
Member

igr commented Aug 11, 2017

Yeees :) I just want to try to wrap CallableStatement, as it looks it should work; then getQueryString would return more information in debug mode... I made a change, just some strange call to close() is not working.

@igr igr closed this as completed in 015b1de Aug 14, 2017
@igr
Copy link
Member

igr commented Aug 14, 2017

Found an issue in Proxetta, thanx to this bug. Quite an important one... it looks like no one uses it, ha ha ;)

Thanx @moh-sushi !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants