Reputation: 528
I actually want to query a JSON-String which is stored in an Oracle Database, using the construct JSON_TABLE. This works pretty well.
SQL Query
SELECT f.val
from JSON,
JSON_TABLE(json,'$' COLUMNS(val VARCHAR(4000) PATH '$.glossary.GlossDiv.GlossList.GlossEntry.GlossTerm')) as f
JSON-String in DB
(It is by the way the example JSON from json.org/example.html)
{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
Now I want execute the query in a normal Java Application. I use it simultaneously in five Threads. That is how I can reproduce the problem. In my original use case I press a button really quickly on a website which executes this query.
JsonRunnable.java
public class JsonRunnable implements Runnable {
public void run() {
try {
List<String> list = new ArrayList<String>();
java.util.Properties connProperties = new java.util.Properties();
connProperties.put("user", "");
connProperties.put("password", "");
Class.forName("oracle.jdbc.driver.OracleDriver");
String database =
"jdbc:oracle:thin:@myserver.com:1521/DB";
Connection conn = DriverManager.getConnection(database, connProperties);
String sql = "SELECT f.val from JSON, JSON_TABLE(json,'$' COLUMNS(val VARCHAR(4000) PATH '$.glossary.GlossDiv.GlossList.GlossEntry.GlossTerm')) as f";
PreparedStatement s1 = conn.prepareStatement(sql);
s1.execute(sql);
ResultSet rs = s1.getResultSet();
while (rs.next()) {
list.add(rs.getString(1));
}
s1.close();
conn.close();
System.out.println(list.get(0));
} catch (Exception ex) {
System.out.println(ex);
}
}
}
Main.java
public class Main {
public static void main(String[] args) {
for(int i = 0;i < 5;i++){
new Thread(new JsonRunnable()).start();
}
}
}
Now, I get this error, which tells me that something failed during parsing and processing the XML (The error message is in german but you can see the ORA Error Message):
java.sql.SQLException: ORA-19114: XPST0003 - Fehler beim Parsen des XQuery Ausdrucks:
ORA-19202: Fehler bei XML-Verarbeitung
jzntSCV1:invState2 aufgetreten
Oracle Driver: OJDBC 7 12.1.0.1
Java: 1.8
Oracle DB: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Can someone help me in this case? I am actually really lost how to solve this problem. Thank you guys a lot!
Upvotes: 5
Views: 2187
Reputation: 436
Have you installed the latest patch set (Patch 24968615: DATABASE PROACTIVE BUNDLE PATCH 12.1.0.2.170117). This should fix the issue.
Upvotes: 2
Reputation: 2155
I did run sql:
select f.val from (
select '{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}'
as json from dual) v
,JSON_TABLE(json,'$' COLUMNS(val VARCHAR(4000) PATH '$.glossary.GlossDiv.GlossList.GlossEntry.GlossTerm')) as f
And it is correct. Then I did make app, as well as you do. And try 12.0.1.1 and 12.0.1.2 drivers. Both working well. App is correct as well as sql.
The only idea that I have about problem is that your json table has different json-schemas or nulls in different rows. Try change you sql to filter by rowid, where row has string as you show above and test app again. Probably when you check sql in IDE (let say Pl/SQl developer) it select only first rows, that why it works, but when you run it in app it try select all rows at once and select this other (or nulls) schemas rows.
Upvotes: 1