java sql exception on DB2, Cursor state not valid - java

Here is my source code.
This source code has no problem.
but sometimes, I met a problem with 'Cursor state not valid' like below exception message.
20211214082151/SQL SELECT/com.dao.TransactionDetailDAO/getDataList()/Cursor state not valid.
This problem has been left unresolved for too long.
Please someone help me. Why this problem happening? and How to solve this problem.
The Point is 'sometimes'. This exception happened around once in fifty.
public Vector getDataList(int arg1, int arg2, long arg3, int arg4) {
Vector vList = new Vector();
try {
m_conn = CSDBConnection.getConnection();
String sQuery = "select DATA1,DATA2 from " + TABLE + " where ARG1= ? and ARG2=? and ARG3=? and ARG4 =? ";
m_pstmt = m_conn.prepareStatement(sQuery);
int i = 0;
m_pstmt.setInt(++i, arg1);
m_pstmt.setInt(++i, arg2);
m_pstmt.setLong(++i, arg3);
m_pstmt.setInt(++i, arg4);
m_rs = m_pstmt.executeQuery();
while (m_rs.next()) {
String[] aResult = new String[2];
aResult[0] = Integer.toString(m_rs.getInt("DATA1"));
aResult[1] = m_rs.getString("DATA2");
vList.add(aResult);
}
} catch (Exception e) {
ErrorLog.writeLog(ErrorLog.ERR_DELETE_SQL, "TransactionDetailDAO", "getDataList", e.getMessage());
} finally {
CSDBConnection.cleanup(m_rs, m_pstmt);
}
return vList;
}
detail exception logs
TimeStamp: / 2021-12-16 05:01:00
Error Message : Internal driver error. (class java.lang.NullPointerException) Error Cause : java.lang.NullPointerException
Error toString : java.sql.SQLException: Internal driver error. (class java.lang.NullPointerException)
StackTraceElement Index : 0 FileName : JDError.java LineNumber : 985 ClassName : com.ibm.as400.access.JDError MethodName : createSQLExceptionSubClass
StackTraceElement Index : 1 FileName : JDError.java LineNumber : 610 ClassName : com.ibm.as400.access.JDError MethodName : throwSQLException
StackTraceElement Index : 2 FileName : JDError.java LineNumber : 568 ClassName : com.ibm.as400.access.JDError MethodName : throwSQLException
StackTraceElement Index : 3 FileName : AS400JDBCConnectionImpl.java LineNumber : 3163 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : sendAndReceive
StackTraceElement Index : 4 FileName : AS400JDBCStatement.java LineNumber : 1805 ClassName : com.ibm.as400.access.AS400JDBCStatement MethodName : commonPrepare
StackTraceElement Index : 5 FileName : AS400JDBCPreparedStatementImpl.java LineNumber : 357 ClassName : com.ibm.as400.access.AS400JDBCPreparedStatementImpl MethodName : <init>
StackTraceElement Index : 6 FileName : AS400JDBCConnectionImpl.java LineNumber : 2313 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : prepareStatement
StackTraceElement Index : 7 FileName : AS400JDBCConnectionImpl.java LineNumber : 2084 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : prepareStatement
StackTraceElement Index : 8 FileName : AS400JDBCConnectionImpl.java LineNumber : 2079 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : prepareStatement
StackTraceElement Index : 9 FileName : Atk_MiscDao.java LineNumber : 37 ClassName : com.connector.dao.mes.Atk_MiscDao MethodName : getMiscInfo2ByTable
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TimeStamp: / 2021-12-16 07:04:56
Error Message : Communication link failure. (Disconnect request received, connection terminated.) Error Cause : com.ibm.as400.access.ConnectionDroppedException: Disconnect request received, connection terminated.
Error toString : java.sql.SQLNonTransientConnectionException: Communication link failure. (Disconnect request received, connection terminated.)
StackTraceElement Index : 0 FileName : JDError.java LineNumber : 884 ClassName : com.ibm.as400.access.JDError MethodName : createSQLExceptionSubClass
StackTraceElement Index : 1 FileName : JDError.java LineNumber : 610 ClassName : com.ibm.as400.access.JDError MethodName : throwSQLException
StackTraceElement Index : 2 FileName : JDError.java LineNumber : 568 ClassName : com.ibm.as400.access.JDError MethodName : throwSQLException
StackTraceElement Index : 3 FileName : AS400JDBCConnectionImpl.java LineNumber : 3147 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : sendAndReceive
StackTraceElement Index : 4 FileName : AS400JDBCStatement.java LineNumber : 1805 ClassName : com.ibm.as400.access.AS400JDBCStatement MethodName : commonPrepare
StackTraceElement Index : 5 FileName : AS400JDBCPreparedStatementImpl.java LineNumber : 357 ClassName : com.ibm.as400.access.AS400JDBCPreparedStatementImpl MethodName : <init>
StackTraceElement Index : 6 FileName : AS400JDBCConnectionImpl.java LineNumber : 2313 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : prepareStatement
StackTraceElement Index : 7 FileName : AS400JDBCConnectionImpl.java LineNumber : 2084 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : prepareStatement
StackTraceElement Index : 8 FileName : AS400JDBCConnectionImpl.java LineNumber : 2079 ClassName : com.ibm.as400.access.AS400JDBCConnectionImpl MethodName : prepareStatement
StackTraceElement Index : 9 FileName : Atk_MiscDao.java LineNumber : 37 ClassName : com.connector.dao.mes.Atk_MiscDao MethodName : getMiscInfo2ByTable
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Related

There is a way to send json string or even JSONObject to POSTMAN without a slash before every double quote?

After alot of research i knew that JAVA does that internally. Tried to replace the slashes with empty string .Also tried to use some libraries to parse String to JSONObject But Same result, A slash before every double qoute..
Request To POSTMAN:
{
"MTI": "0100",
"2": "4655206331051889",
"3": "000000",
"4": "000000012300",
"7": "0321054133",
"11": "001205",
"14": "0325",
"18": "5399",
"22": "022",
"25": "00",
"35": "2312312332",
"37": "206305000014",
"41": "29110001",
"42": "1001001",
"49": "840",
"transactionid": "12",
"co-ordinates": "3042304,293572945"
}
Code:
StringBuilder transactionReq = new StringBuilder();
for (Object o : responseMessage.getChildren().keySet()) {
int key = (Integer) o;
// The Transaction Request Body that has been Received in JSON Format.
transactionReq
.append('"')
.append(key)
.append('"')
.append(" : ")
.append('"')
.append(responseMessage.getValue(key))
.append('"')
.append(" ,");
}
transactionReq
.insert(0, "{")
.deleteCharAt(transactionReq.length() - 1)
.deleteCharAt(transactionReq.length() - 1)
.insert(transactionReq.length(), "}");
response.setMessage(transactionReq.toString().replaceAll("\\\\", ""));
System.out.println(transactionReq.toString());
Console:
{
"message": "{"0" : "0110" ,"1" : "4655206331051889" ,"3" : "000000" ,"4" : "000000012300" ,"6" : "000000000012" ,"7" : "0321054133" ,"11" : "001205" ,"14" : "0325" ,"18" : "5399" ,"22" : "022" ,"25" : "00" ,"35" : "2312312332" ,"37" : "549684 " ,"38" : "84738 " ,"39" : "00" ,"41" : "29110001" ,"42" : "1001001 " ,"49" : "840" ,"57" : "3042304" ,"58" : "293572945"}"
}
Response From POSTMAN:
{
"message": "{\"0\" : \"0110\" ,\"2\" : \"4655206331051889\" ,\"3\" : \"000000\" ,\"4\" : \"000000012300\" ,\"6\" : \"000000000012\" ,\"7\" : \"0321054133\" ,\"11\" : \"001205\" ,\"14\" : \"0325\" ,\"18\" : \"5399\" ,\"22\" : \"022\" ,\"25\" : \"00\" ,\"35\" : \"2312312332\" ,\"37\" : \"549684 \" ,\"38\" : \"84738 \" ,\"39\" : \"00\" ,\"41\" : \"29110001\" ,\"42\" : \"1001001 \" ,\"49\" : \"840\" ,\"57\" : \"3042304\" ,\"58\" : \"293572945\"}"
}
The Output in the console proves that iam sending a right json request..
But the response the shows in postman says the opposite..
Happy to hear any Explanation...
Thanks in Advance
It`s built in java, Java treats Strings that way , So i used a DTO To avoid that

Splitting a string into an array then splitting the array again

I have this string:
fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862
I want to use a loop to split this string into an array at the comma [,] example:
[0]fname lname
[1]GTA V: 120 : 00000
[2]Minecraft : 20 : 10
[3]Assassin’s Creed IV : 90 : 800
[4]Payday 2 : 190 : 2001
[5]Wolfenstein TNO : 25 : 80
[6]FarCry 4 : 55 : 862
Then I want to use another loop to split this further at : into another array example
[0]fname lname
[1]GTA V
[2]120
[3]00000
[4]Minecraft
[5]20
[6]10
....
Is there a better way of doing this?
currently I have:
List<String> lines = new ArrayList<String>();
while (scan.hasNextLine())
{
lines.add(scan.nextLine());
}
//converts the list array to string array
String[] scanarray = lines.toArray(new String[0]);
//converts the string array into one large string
String str_array = Arrays.toString(scanarray);
String[] arraysplit;
arraysplit = str_array.split("\\s*:\\s*");
for (int i=0; i<arraysplit.length; i++)
{
arraysplit[i] = arraysplit[i].trim();
// ^^^^^^^^^^^^ has values with spaces
System.out.println(scanarray[i]);
}
EDIT:
Currently my program creates 3 identical arrays, containing the example you can see in the second block of code above.
You can use the split method from String class with multiple delimiters
public static void main(String[] args) {
String myOriginalString = " fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862";
// | is the regex OR operator
String[] splited = myOriginalString.split(",|:");
for(String s : splited)
System.out.println(s.trim());
}
You can achieve it what you are looking for with REGEX, just put what all thing you get separated with string split method.
I tried below code locally and it is pretty much same what you are looking for.
public class StackSol1 {
public static void main(String[] args) {
String str = "fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 :80, FarCry 4 : 55 : 862";
String delimiters = "\\s+|,\\s*|\\:\\s*";
// analyzing the string
String[] tokensVal = str.split(delimiters);
// prints the number of tokens
System.out.println("Count of tokens = " + tokensVal.length);
String finalStr="";
for (String token : tokensVal) {
finalStr = finalStr+"\n"+token;
}
System.out.println(finalStr);
}
}
How about using split with regex? e.g.
String aa = "fname lname, GTA V: 120 : 00000, Minecraft : 20 : 10, Assassin’s Creed IV : 90 : 800, Payday 2 : 190 : 2001 ,Wolfenstein TNO : 25 : 80, FarCry 4 : 55 : 862";
String [] a = aa.split("[,:]");

Error in MapReduce command with MongoDB Java API

I'm testing the MongoDB Java API and I wanted to do a mapReduce.
I implemented it as follow :
String map = "function() { " +
"emit(this.ts, this.1_bid);}";
String reduce = "function(key, values) {" +
"return Array.sum(values);}";
MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput out = collection.mapReduce(cmd);
for (DBObject o : out.results()) {
System.out.println(o.toString());
}
But when I execute it I have the following exception stack :
[tick_engine] 16:51:53.600 ERROR [MongoTickDataReader] Failed to read data from mongoDB
com.mongodb.CommandFailureException: { "serverUsed" : "/127.0.0.1:27017" , "errmsg" : "exception: SyntaxError: Unexpected token ILLEGAL" , "code" : 16722 , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:71) ~[mongo-2.11.1.jar:na]
at com.mongodb.CommandResult.throwOnError(CommandResult.java:110) ~[mongo-2.11.1.jar:na]
at com.mongodb.DBCollection.mapReduce(DBCollection.java:1265) ~[mongo-2.11.1.jar:na]
at com.smarttrade.tickEngine.in.MongoTickDataReader.mapReduce(MongoTickDataReader.java:321) ~[classes/:na]
at com.smarttrade.tickEngine.in.MongoTickDataReader.readData(MongoTickDataReader.java:157) ~[classes/:na]
at com.smarttrade.tick.engine.TickEngine.onMarketDataRequest(TickEngine.java:203) [classes/:na]
at com.smarttrade.tick.sttp.TickMarketDataRequestCommand.execute(TickMarketDataRequestCommand.java:62) [classes/:na]
at com.smarttrade.st.commands.Command.process(Command.java:140) [src/:na]
at com.smarttrade.st.server.STTPInvoker$1.process(STTPInvoker.java:385) [src/:na]
at com.smarttrade.st.server.STTPInvoker$1.process(STTPInvoker.java:1) [src/:na]
at com.smarttrade.util.concurrent.queue.MultiSessionsBlockingQueue$SimpleSession.run(MultiSessionsBlockingQueue.java:122) [src/:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_51]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_51]
at java.lang.Thread.run(Thread.java:744) [na:1.7.0_51]
The problem seems to be with the attribute name that you have defined - 1_bid
I created sample documents to test your map-reduce -
{ "_id" : ObjectId("533ef7d0e1687dd644410d88"), "ts" : "TSKEY", "1_bid" : 200 }
{ "_id" : ObjectId("533ef7d3e1687dd644410d89"), "ts" : "TSKEY", "1_bid" : 300 }
{ "_id" : ObjectId("533ef7d5e1687dd644410d8a"), "ts" : "TSKEY", "1_bid" : 400 }
{ "_id" : ObjectId("533ef7dce1687dd644410d8b"), "ts" : "TSKEY2", "1_bid" : 800 }
{ "_id" : ObjectId("533ef7dfe1687dd644410d8c"), "ts" : "TSKEY2", "1_bid" : 300 }
I ran following map-reduce command -
db.sample4.mapReduce(function() { emit(this.ts, this.1_bid);},function(key, values) {return Array.sum(values);})
The error that I got is SyntaxError: missing ) after argument list (shell):1
I realized, that the function, that mapper is executing, is a JavaScript function and in Javascript, you cannot have a variable that starts with a number. Hence you get a syntax error. I then created new set of documents -
{ "_id" : ObjectId("533eff29e1687dd644410d8d"), "ts" : "TSKEY", "bid_1" : 200 }
{ "_id" : ObjectId("533eff2de1687dd644410d8e"), "ts" : "TSKEY", "bid_1" : 300 }
{ "_id" : ObjectId("533eff34e1687dd644410d8f"), "ts" : "TSKEY", "bid_1" : 400 }
{ "_id" : ObjectId("533eff7fe1687dd644410d92"), "ts" : "TSKEY2", "bid_1" : 800 }
{ "_id" : ObjectId("533eff85e1687dd644410d93"), "ts" : "TSKEY2", "bid_1" : 300 }
and then modified the mapper to use "bid_1" and ran the following command -
db.sample4.mapReduce(function() { emit(this.ts, this.bid_1);},function(key, values) {return Array.sum(values);},"pivot")
The output was -
{
"result" : "pivot",
"timeMillis" : 61,
"counts" : {
"input" : 12,
"emit" : 12,
"reduce" : 2,
"output" : 2
},
"ok" : 1,
}
db.pivot.find()
{ "_id" : "TSKEY", "value" : 900 }
{ "_id" : "TSKEY2", "value" : 1100 }
I tested this in Java using the same program that you have pasted and just changed the attribute name to "bid_1" and it worked
To prevent syntax errors on field names, you can also write the map function this way :
function() {
emit(this["ts"], this["1_bid"]);
}

Android - Parse JSON using GSON

I would like to parse data from JSON which is String type. I am using Google Gson. I'm wondering how can I get "OriginalTerm" and "FirstTranslation" information of this Json String:
{
"term0" : {
"PrincipalTranslations" : {
"0" :{
"OriginalTerm" : { "term" : "cat", "POS" : "n", "sense" : "domestic animal", "usage" : ""},
"FirstTranslation" : {"term" : "gato", "POS" : "nm", "sense" : " "}, "Note" : ""},
"1" :{
"OriginalTerm" : { "term" : "cat", "POS" : "n", "sense" : "member of cat family", "usage" : ""},
"FirstTranslation" : {"term" : "felino", "POS" : "nm", "sense" : "familia de animales"}, "Note" : ""}},
"AdditionalTranslations" : {
"0" :{
"OriginalTerm" : { "term" : "cat", "POS" : "n", "sense" : "guy", "usage" : "slang"},
"FirstTranslation" : {"term" : "tío, tipo, chaval", "POS" : "nm", "sense" : "coloq"},
"SecondTranslation" : {"term" : "vato", "POS" : "", "sense" : "Mex"}, "Note" : ""},
"original" : {
"Compounds" : {
"0" :{
"OriginalTerm" : { "term" : "alley cat", "POS" : "n", "sense" : "stray cat", "usage" : ""},
"FirstTranslation" : {"term" : "gato callejero", "POS" : "nm", "sense" : ""}, "Note" : ""},
"Lines" : "End Reached", "END" : true
}
I tried following these information but I can't solve it:
http://albertattard.blogspot.com.es/2009/06/practical-example-of-gson.html
JSON parsing using Gson for Java
I tried to serialized using GSON using POJO but I can't find the right structure, I tried using JsonObject too, jumping through object keys like "term0","PrincipalTranslations" but I have some trouble when I've multiple results for the same key, for example:
"0" :{
"OriginalTerm"....
"FirstTranslation"...
"1" :{
"OriginalTerm"....
"FirstTranslation"...
}
Thank you for advance.
Parsing a JSON with Gson can be done using a POJO that maps one-to-one your JSON text with your object. But, since you need only part of the JSON string, you can take advantage of JsonParser object that allows you to get a portion only of your JSON.
So you can get PrincipalTranslation part and then apply the POJO strategy keeping in mind that you have at least two structures: your Term and a composition of two Terms and a note (that I called Item).
Keep in mind that POJO I write are not following Java naming convention, so you can add an annotation to use a different member variable name.
Here's a code you can paste and run in you IDE to try.
package stackoverflow.questions;
import java.lang.reflect.Type;
import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
public class Q20337652 {
public static class Term {
String term;
String POS;
String sense;
String usage;
#Override
public String toString() {
return "Term [term=" + term + ", POS=" + POS + ", sense=" + sense + ", usage=" + usage + "]";
}
}
public static class Item {
Term OriginalTerm;
Term FirstTranslation;
String Note;
#Override
public String toString() {
return "Item [OriginalTerm=" + OriginalTerm + ", FirstTranslation=" + FirstTranslation + ", Note=" + Note + "]";
}
}
public static void main(String[] args){
String json =
" { "+
" "+
" \"term0\" : { "+
" \"PrincipalTranslations\" : { "+
" \"0\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"domestic animal\", \"usage\" : \"\"}, "+
" \"FirstTranslation\" : {\"term\" : \"gato\", \"POS\" : \"nm\", \"sense\" : \" \"}, \"Note\" : \"\"}, "+
" \"1\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"member of cat family\", \"usage\" : \"\"}, "+
" \"FirstTranslation\" : {\"term\" : \"felino\", \"POS\" : \"nm\", \"sense\" : \"familia de animales\"}, \"Note\" : \"\"}},"+
" \"AdditionalTranslations\" : { "+
" \"0\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"guy\", \"usage\" : \"slang\"}, "+
" \"FirstTranslation\" : {\"term\" : \"tío, tipo, chaval\", \"POS\" : \"nm\", \"sense\" : \"coloq\"}, "+
" \"SecondTranslation\" : {\"term\" : \"vato\", \"POS\" : \"\", \"sense\" : \"Mex\"}, \"Note\" : \"\"}, "+
" "+
" \"original\" : { "+
" \"Compounds\" : { "+
" \"0\" :{ "+
" \"OriginalTerm\" : { \"term\" : \"alley cat\", \"POS\" : \"n\", \"sense\" : \"stray cat\", \"usage\" : \"\"}, "+
" \"FirstTranslation\" : {\"term\" : \"gato callejero\", \"POS\" : \"nm\", \"sense\" : \"\"}, \"Note\" : \"\"}, "+
" \"Lines\" : \"End Reached\", \"END\" : true "+
" "+
" } "+
" } }}} ";
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json);
JsonElement je2 = je.getAsJsonObject().get("term0");
JsonElement je3 = je2.getAsJsonObject().get("PrincipalTranslations");
Type mapType = new TypeToken<Map<String, Item>>() {}.getType();
Map<String, Item> principalTranslation = new Gson().fromJson(je3, mapType);
System.out.println(principalTranslation);
}
}
And this is my execution:
{0=Item [OriginalTerm=Term [term=cat, POS=n, sense=domestic animal, usage=], FirstTranslation=Term [term=gato, POS=nm, sense= , usage=null], Note=],
1=Item [OriginalTerm=Term [term=cat, POS=n, sense=member of cat family, usage=], FirstTranslation=Term [term=felino, POS=nm, sense=familia de animales, usage=null], Note=]}
I think this could help: http://www.newthinktank.com/2013/07/android-development-15/
It's a video tutorial explaining the how to use json. Not through GSON though, but I think you need a base on how JSON works before getting involved in GSON (it's rather complicated)

Java MongoDb : Dot Notation

{ "_id" : { "$oid" : "52568424036439f2c5107571"} , "start" : { "x:" : 71 , "y:" : 9} , "end" : { "x:" : 30 , "y:" : 84}}
{ "_id" : { "$oid" : "52568424036439f2c5107572"} , "start" : { "x:" : 28 , "y:" : 59} , "end" : { "x:" : 72 , "y:" : 64}}
{ "_id" : { "$oid" : "52568424036439f2c5107573"} , "start" : { "x:" : 16 , "y:" : 71} , "end" : { "x:" : 18 , "y:" : 79}}
Need all docs say where start.x > 40
Query tried mongoDb console :
db.lines.find({"start.x": {$gt:40} })
Java Driver:
DBObject query = QueryBuilder.start("start.x").greaterThan(50).get();
collection.find(query)
Both cases no error, no documents retrieved.
You named your field x: not x so this should do the work:
db.lines.find({"start.x:": {$gt:40} })
Haven't used Mongo with Java, but... have you tried using QueryBuilder.get instead of QueryBuild.start? That's the method used for find operations.
You can also use a more specific one for this case: QueryBuild.greaterThan

Categories