Discussions

Web tier: servlets, JSP, Web frameworks: Writing to BLOB

  1. Writing to BLOB (5 messages)

    Hi
     I have a JSP file where user can upload files to the server. This JSP calls a servlet which does the uploading, after the file is uploaded I call EJB which inerserts the file in to Database. The problem is I there is no exception or error but the file is not insertred in the database.
    The EJB method is like this.

      public boolean insertTemplate (String ,java.io.InputStream oData,String sName)
      {
            makeConnection();//Open Connection to database
            int update = 0;

            try {
                 con.setAutoCommit(false);
                 sql ="insert into HTML_TEMPLATE values(HTML_TEMPLATE_SEQ.nextval,'" + sName + "',EMPTY_BLOB(),'" + sPid + "')";
                 update = stm.executeUpdate(sql);

                 java.sql.Blob myBlob = null;
                 Statement stmt2 = con.createStatement();
                 stmt2.execute("SELECT TEMPLATE from HTML_TEMPLATE");
                 ResultSet rs = stmt2.getResultSet();
                 while (rs.next())
                  {
                   myBlob = rs.getBlob("TEMPLATE");
                  }

             java.io.InputStream is = oData;
             System.out.println("The InputStream in EJB is :" + is);
             java.io.OutputStream os = ((weblogic.jdbc.common.OracleBlob) myBlob).getBinaryOutputStream();

              byte[] inBytes = new byte[65534];
              int numBytes = is.read(inBytes);

              while (numBytes > 0)
              {
                os.write(inBytes, 0, numBytes);
                numBytes = is.read(inBytes);
              }
                 //Clean up
                 os.write(inBytes);
                 os.flush();
                 os.close();
                 stmt2.close();


            } catch (Exception e) {
                System.out.println("EJB - An exception has been raised during the insertion of the Template details" + e.getMessage());
            }
            finally
            {
             try
               {
                con.commit();
                closeConnection();
               }
             catch(Exception ex)
               {
                ex.toString();
               }
            }

            if(update == 0)
            {
            return false;
            }
            else
            {
             return true;
            }
        } //End of insert template method


    Please help me with this.
    ThankYou
    Asad

    Threaded Messages (5)

  2. Writing to BLOB[ Go to top ]

    Make sure the TX attribute of your EJB method is REQUIRED.
  3. Writing to BLOB[ Go to top ]

    ThankYou for your reply
     The Transaction is REQUIRED in the EJB but, I dont know where I am doing wrong. I cannot insert the files in the database. What am I missing in the code?

    Asad

  4. Writing to BLOB[ Go to top ]

    I would do the following:

    1. Change the following line to
    stmt2.execute("SELECT TEMPLATE from HTML_TEMPLATE FOR UPDATE");
    2. Remove the transaction management code such as con.setAutoCommit(false) and con.commit and let the EJB container take care of it.
  5. Writing to BLOB[ Go to top ]

    Thanks Eric
      I removed the two lines, the transaction code and now its working. Thankyou for yr help
    Bye
    Asad
  6. Re: Writing to BLOB[ Go to top ]

    Hi, I Have a probelm of getting data as a stream. When i try the above code system just hangs. My need is to store image files in a database column. Thanks in advance