0

I am using JSF 1.2 and am trying to use <a4j:keepAlive beanName="reportController">, but I keep on getting this error:

HTTP Status 500

Caused by: java.io.NotSerializableException: com.mysql.jdbc.DatabaseMetaData at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183

I am trying to use <a4j:keepAlive beanName="reportController"> because when I search for a specific report and then try to sort the data in the dataTable, it seems that it loses all the data in the dataTable.

Community
  • 1
  • 1
user3278647
  • 494
  • 1
  • 4
  • 9
  • 500 is an internal server error. Look into your logfiles – Jens Feb 03 '16 at 07:20
  • Caused by: java.io.NotSerializableException: com.mysql.jdbc.DatabaseMetaData at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183 – user3278647 Feb 03 '16 at 07:22
  • Add the stacktrace and the relevant code to your question using the edit funktion. – Jens Feb 03 '16 at 07:23

1 Answers1

2

Caused by: java.io.NotSerializableException: com.mysql.jdbc.DatabaseMetaData

This will happen when you get hold of java.sql.Connection or even directly DatabaseMetaData as instance variable of a serializable class like below.

public class ReportController implements Serializable {

    private Connection connection; // BAD!!
    private DatabaseMetaData metadata; // BAD!!

    // ...
}

You're not supposed to declare and get hold of external resources such as java.sql.Connection, Statement and ResultSet nor its properties as instance variables of a class. You should acquire, use and close them as soon as possible, exclusively within the method local scope. Get rid of those instance variables from the ReportController bean, move them into method local scope and this problem shall disappear. Only having DataSource (the server-managed connection pool) as instance variable is OK.

public class ReportController implements Serializable {

    @Resource("jdbc/someDB")
    private DataSource dataSource;

    public void someMethod() {
        try (Connection connection = dataSource.getConnection()) { // OK.
            // ...
        }
    }

    // ...
}

The <a4j:keepAlive> isn't exactly the cause of this problem. It just remembers the bean instance in the HTTP session across HTTP postback requests on the same page. HTTP session attributes are inherently usually serialized. This serialization merely triggered and exposed your hidden design problem. Volatile one-time-use resources such as database connection, statement, metadata, inputstream, outputstream, etc are absolutely not supposed to be serializable and hence this exception.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have a DbUtil Class that has a method that returns a Connection object so that i handle connection using one class . public Connection getConnection() {} – user3278647 Feb 03 '16 at 08:06
  • Exception tells you're holding of it as an instance variable somewhere as demonstrated in 1st code snippet. This is bad design. If you want more detail, see the 1st "See also" link. – BalusC Feb 03 '16 at 08:09
  • It has worked. Thank you ,will start applying this new approach. – user3278647 Feb 03 '16 at 08:14