1

I am using the EntityManager from javax.persistence to do some operations on a informix database. For example:

public void execute(String sql) {
    Query query = entityManager.createNativeQuery(sql);
    query.executeUpdate();
}

I need the SQLCODE returned by this execution. Do you have any hints for me on how to achieve this?

Edit: I found out that it is possible to use SQLCA (SQL Communication Area). Has anyone experience with that?

Chris311
  • 3,794
  • 9
  • 46
  • 80
  • Why do you need the return code? "executeUpdate" will throw an exception if the update fails. otherwise it worked well. – Si mo Nov 09 '15 at 10:15
  • My product owner want's this. All executed sql's are stored in a database table. One column is provided for the sqlcode..I think he want's to differ between the various sqlReturnCodes... – Chris311 Nov 09 '15 at 10:25
  • i would write something like that: public void execute(String sql) { int returncode=0; try { Query query = entityManager.createNativeQuery(sql); query.executeUpdate(); }catch(sqlException e) { returncode = e.getErrorCode(); } } – Si mo Nov 09 '15 at 11:05
  • There is no such exception thrown by the entityManager... – Chris311 Nov 09 '15 at 12:32
  • But executeUpdate can throw a RuntimeException. http://docs.oracle.com/javaee/7/api/javax/persistence/Query.html#executeUpdate-- – Si mo Nov 09 '15 at 12:39
  • Throws: IllegalStateException - if called for a Java Persistence query language SELECT statement or for a criteria query TransactionRequiredException - if there is no transaction or the persistence context has not been joined to the transaction QueryTimeoutException - if the statement execution exceeds the query timeout value set and only the statement is rolled back PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back – Si mo Nov 09 '15 at 12:39
  • Yes, but they got no getErrorCode()-method. – Chris311 Nov 09 '15 at 13:14
  • perhaps there is a sqlexcecption in the cause of the runtimeexception? the sql exception has a method getErrorCode. .... sorry I have no other idea – Si mo Nov 09 '15 at 13:18
  • Yeah, that would be possible, but it is really ugly...I need to cast and check for instanceOf and stuff and I need to rethrow the exception, because I wan't my program to abort in such a case. – Chris311 Nov 09 '15 at 15:41
  • Yes it is ... but I don't see another solution. Sorry. – Si mo Nov 10 '15 at 09:07
  • I think I found another way: look my edited post. – Chris311 Nov 10 '15 at 10:37
  • The SQLCode can be obtained from the `SQLException` that was thrown by the driver. But apparently your obfuscation layer does not let you access that. –  Nov 10 '15 at 10:45
  • Yes, it is hidden deep inside the cause. But I found out, that it is possible to ask the db for the error code. I just don't know how to do this. – Chris311 Nov 10 '15 at 10:49

0 Answers0