0

I have DAO when I make DAO Serializable I get exception :

 Cannot serialize session attribute com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap for session C354B1B6053088CBB8E8A933E5F8EAE0
    java.io.NotSerializableException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper

my DAO:

public boolean getConnection() {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("java:/comp/env/jdbc/Orcl");
            connection = dataSource.getConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(KPIDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (NamingException ex) {
            Logger.getLogger(KPIDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

In controller (@ViewScoped) :

KPIDAO kpiDAO = new KPIDAO();

Context.xml

<Resource 
        name="jdbc/Orcl" 
        auth="Container"
        type="javax.sql.DataSource" 
        username="username" 
        password="password"
        driverClassName="oracle.jdbc.OracleDriver" 
        url="jdbc:oracle:thin:@192.168.1.10:1521:XE"
        maxActive="8" 
   />

What is proper solution ?

kinkajou
  • 3,664
  • 25
  • 75
  • 128

2 Answers2

1

For example

@ViewScoped
public class ViewBean implements Serializable{
  private transient KPIDAO kpiDAO = //get singleton instance from factory
  //also dont keep connection object as instance variable, fetch it from pool in methods and perform db operations.

  private void readObject(java.io.ObjectInputStream stream)
        throws java.io.IOException, ClassNotFoundException
    {
        stream.defaultReadObject();

        // assign reference manually.
        this.kpiDAO  =  //get from factory;
    }

private void writeObject(java.io.ObjectOutputStream stream)
        throws java.io.IOException
    {

        stream.defaultWriteObject();
    }
}

Note: In case you move connection object from instance variable to local method variables, you won't need above code too.

Update:

From your code snippet and exception

connection = dataSource.getConnection();

It seems its an instance variable.

i.e.

public class Dao {
 private Connection connection; //instance variable
}

change it to

public class Dao {

 public List<Bean> getResult(){
  //local method variable
  Connection connection = //get from pool
  //perform db operation with connection object
 }

}

baba.kabira
  • 3,111
  • 2
  • 26
  • 37
  • +1 @gbagga really nice. Any tutorials :) I really didn't knew this and "move connection object from instance variable to local method variables" means? – kinkajou Jul 20 '12 at 08:07
0

You should not be storing a reference to the PoolingDataSource in the session, as PoolingDataSource is not serializable. I would suggest you store it somewhere else, e.g. in the application context.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66