OpenJPA query fails second time (possibly because of byte array parameter)

吃可爱长大的小学妹 提交于 2019-12-11 04:07:46

问题


I have following OpenJPA query (over db2 database):

Select number from Number number where number.uuid = :uuid

Column uuid defined as VARCHAR FOR BIT DATA(16). In entity it represented as byte[] type field.

The problem is that when I call method with that query first time(after WebSphere server is up) it works fine. Consequential calls result in error (I even enabled trace log see where the error is)

1st time:> query is running, everything's ok
[11/25/15 13:27:03:803 IST] 0000001d Query         3   openjpa.Query: Trace: Executing query: [Select n from Number n where n.uuid = :uuid] with parameters: ?
[11/25/15 13:27:04:199 IST] 0000001d jdbc_SQL      3   openjpa.jdbc.SQL: Trace: <t 507109353, conn 1755254015> executing prepstmnt -1123134307 SELECT t0.NUMBER_ID, t0.comment, t0.EMPLOYEE_ID, t0.number, t0.RANGE_ID, t0.status, t0.uuid FROM PHONES.Number t0 WHERE (t0.uuid = ?)  [params=?]
[11/25/15 13:27:04:249 IST] 0000001d jdbc_SQL      3   openjpa.jdbc.SQL: Trace: <t 507109353, conn 1755254015> [50 ms] spent
[11/25/15 13:27:04:251 IST] 0000001d jdbc_JDBC     3   openjpa.jdbc.JDBC: Trace: <t 507109353, conn 1755254015> [0 ms] close
2nd time:>
[11/25/15 13:27:09:068 IST] 0000001d Query         3   openjpa.Query: Trace: Executing query: [Select n from Number n where n.uuid = :uuid] with parameters: ?
[11/25/15 13:27:09:556 IST] 0000001d BusinessExcep E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "approve" on bean "BeanId(PhoneAllocationEAR#PhoneAllocationEJB.jar#RangeProvider, null)". Exception data: <openjpa-2.1.2-SNAPSHOT-r422266:1548248 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Failed to execute query "Select n from Number n where n.uuid = :uuid". Check the query syntax for correctness. See nested exception for details.

This is nested exception:

Caused by: java.lang.NullPointerException
at org.apache.openjpa.jdbc.sql.DB2Dictionary.setBytes(DB2Dictionary.java:1037)
at org.apache.openjpa.jdbc.sql.DBDictionary.setUnknown(DBDictionary.java:1481)
at org.apache.openjpa.jdbc.sql.DBDictionary.setUnknown(DBDictionary.java:1429)
at org.apache.openjpa.jdbc.kernel.PreparedSQLStoreQuery$PreparedSQLExecutor.executeQuery(PreparedSQLStoreQuery.java:114)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1005)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
... 86 more 

This is code that falls with exception:

public String findByUUID(UUID uuid) {

    TypedQuery<Number> query = em.createQuery("Select n from Number n where n.uuid = :uuid", Number.class);
    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    byte[] parameter = bb.array();

    query.setParameter("uuid", parameter);

    List<Number> result = new ArrayList<Number>(query.getResultList());
    return result;
}

*additional info: If I drop BIT DATA from column description - I get another error :

nested exception is: javax.ejb.EJBException: See nested exception; nested exception is: org.apache.openjpa.persistence.PersistenceException: The value of input variable, expression or parameter number "1" cannot be used because of its data type.. SQLCODE=-301, SQLSTATE=07006, DRIVER=4.14.113 {prepstmnt 2039708348 SELECT t0.NUMBER_ID, t0.comment, t0.EMPLOYEE_ID, t0.number, t0.RANGE_ID, t0.status, t0.uuid FROM PHONES.Number t0 WHERE (t0.uuid = ?) [params=?]} [code=-301, state=07006]SQLCA OUTPUT[Errp=SQLRI4A4, Errd=-2145779603, 0, 0, 0, -3700, 0] The value of input variable, expression or parameter number "1" cannot be used because of its data type.. SQLCODE=-301, SQLSTATE=07006, DRIVER=4.14.113 FailedObject: Select n from Number n where n.uuid = :uuid [java.lang.String]

I learned it from looking at source of openJPA but it doesn't help me meanwhile.


回答1:


Try to disable the prepared query cache. It's known to be quite buggy. You can search the OpenJPA JIRA to find details if you want.

<property name="openjpa.jdbc.QuerySQLCache" value="true(EnableStatistics=true)">



来源:https://stackoverflow.com/questions/33915801/openjpa-query-fails-second-time-possibly-because-of-byte-array-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!