Discussions

EJB programming & troubleshooting: Data is not getting saved to the database

  1. Data is not getting saved to the database (2 messages)

    Hi,

    I am new to Java. Wrote code given below to insert data to the Database. Every thing seems to be fine, not getting any exception but data is not getting saved to the database. Please some one suggest me what could be wrong.

     public void insertBatch(Vector list) throws SQLException, DAOException
     {
      logger.debug("insertBatch(): Enter");
      Connection connection = null;
      Statement stmt = null;
      
      try
      {
       int count = 0;
       String sql = null;
       Iterator it = list.iterator();
       connection = DBUtil.getDBConnection();
       stmt = connection.createStatement();
       connection.setAutoCommit(false);

       while(it.hasNext())
       {
        sql = String.valueOf(it.next()).trim();
        stmt.addBatch(sql);
        logger.debug(count + " - BATCH SQL: " + sql);
        count++;
       }
       int [] code = stmt.executeBatch();
       for(int i = 0; i < code.length; i++) System.out.println("BATCH ERROR CODE - " + code[i]);
      }
      catch(ServiceLocatorException e)
      {
       logger.error("ServiceLocatorException:", e);
       throw new DAOException("ServiceLocatorException: Unable to get servicelocator",e);
      }
      catch(SQLException se)
      {
       switch(se.getErrorCode())
       {
        case LogErrorConstants.SQLCODE.DEADLOCK : logger.error("SQL DEADLOCK in insertBatch(): ", se);
         //connection.rollback();
         break;
        case LogErrorConstants.SQLCODE.UNVALIABLE_RESOURCE : logger.error("Unavailable Resource insertBatch: ", se);
         //connection.rollback();
         break;
        case LogErrorConstants.SQLCODE.RECORD_ALREADY_EXIST : logger.error("Duplicate Record insertBatch(): ", se);
         //connection.rollback();
         break;
        default:
         //connection.rollback();
         logger.error("SQL EXCEPTION in insertBatch(): ", se.getNextException());
       }
       throw new SQLException("SQLCode " + se.getErrorCode());
      }
      finally
      {
       connection.commit();
       DBUtil.closeResources(stmt, connection);
       logger.debug("insertBatch(): Exit");
      }
     }

    Thanks in advance

  2. Make sure you are working in a transaction.

  3. What does it mean?[ Go to top ]

    What does it mean? Any more explanation to this. I really need to figure out things out. - Michael Courouleau