3 Replies Latest reply on Jan 4, 2003 12:32 PM by Konrad Neitzel

    BLOB field for CMP

    Ben Litchfield Newbie

      Here is my setup:

      database:postgres 7.2.3
      jboss:3.0.4

      I have a table
      CREATE TABLE discourse_file
      (
      discourse_file_id integer NOT NULL,
      filename varchar (4000),
      contentType varchar (4000),
      file bytea
      )

      My CMP bean looks like this
      ...
      public abstract java.io.InputStream getFile();
      public abstract void setFile(java.io.InputStream file);

      I try to create the bean from a FileInputStream and I get an error shown below. What am I doing wrong here?

      Caused by: javax.ejb.CreateException: Could not create entity:javax.ejb.EJBException: Internal error setting parameters for field file; CausedByException is:
      Can't serialize binary object: java.io.NotSerializableException: java.io.FileInputStream
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.insertEntity(JDBCCreateEntityCommand.java:199)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.execute(JDBCCreateEntityCommand.java:131)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:527)

        • 1. Re: BLOB field for CMP
          Konrad Neitzel Newbie

          Hi!

          > public abstract java.io.InputStream getFile();
          > public abstract void setFile(java.io.InputStream
          > am file);

          > I try to create the bean from a FileInputStream and I
          > get an error shown below. What am I doing wrong
          > here?

          > Caused by: javax.ejb.CreateException: Could not
          > create entity:javax.ejb.EJBException: Internal error
          > setting parameters for field file; CausedByException
          > is:
          > Can't serialize binary object:
          > java.io.NotSerializableException:

          InputStream is not an Object, that can be serialized. Just create a array of Byte or something like that and store them instead.

          Maybe you should also read, what a stream is. A stream is not the data, that are sent through them. It's only an object, that manages a data flow.

          I like to work with java.nio.* Classes like ByteBuffer, but these are only available with jdk 1.4.

          I hope, that my answer helped you.

          With kind regards,

          Konrad

          • 2. Re: BLOB field for CMP
            Ben Litchfield Newbie

            hmmm, that's what I thought I would have to do. As I said I want to be able to store a file, which could be very large and I didn't want to have to load the whole thing into memory. I would think that there has to be a way to do that without loading the entire file into memory.

            • 3. Re: BLOB field for CMP
              Konrad Neitzel Newbie

              Hmm .. If you want to store the whole file inside the database, then you must load data from the file and store it inside the database.

              Another way is: Just store, where the file can be found. This has some advantages and also some disatvanteges.

              One big deal is: You must not load the data and store it inside the database.

              Some real bad things are:
              - Clients need access to the file. (Can be solved, if you store full URLs like http://somehost/my/path/to/file)
              - You cannot force, that data is always ok. So somebody can remove or rename a file and the the links inside the database are no longer working. (When storing inside a database, you can force this reference integrity!)

              It depends on, what you want and need, what way you want to go.

              If you build up some Client <-> Server Applications, the way with links could be good. One of my project does it that way:

              Client connects to a "proxy" via tcp/ip socket
              Proxy uses RMI to JBoss
              (This is done to transfer a minimum of bytes over internet)

              Bigger files will not be stored inside the database. If the client needs the data, the server sends only a URL back. The client must get it on it's own.

              Here comes another advantage: Traffic is a really bad thing for me. I am building up a online game for hundreds of player and traffic could become expensive. But with that I can store the bigger resources for the clients on server, where traffic is less expensive or even completly free (e.g. sourceforge).

              That is an example, why it can be good to store it as files.

              With kind regards,

              Konrad