java.io.NotSerializedException: org.postgresql.jdbc4.Jdbc4Connection

发布于 2024-12-09 19:18:26 字数 857 浏览 1 评论 0原文

我有一个具有视图范围的托管 bean。问题是,当我运行 Web 应用程序时,出现以下错误:

GRAVE: Error Rendering View[/login.xhtml]
java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Connection
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source).........

但是当我将范围更改为会话或请求时,一切正常。 谁能告诉我我是否做错了什么?也许我忘记了配置什么的?

I have a managed bean with view scope. The problem is that when I run the web application I have the following error:

GRAVE: Error Rendering View[/login.xhtml]
java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Connection
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source).........

But when I change the scope to session or request, everything works just fine.
Can anyone tell my if I have done anything wrong? Maybe I forgot a configuration or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-12-16 19:18:26

您已将 JDBC Connection 指定为视图作用域托管 bean 的属性。查看作用域托管 bean 及其所有属性都需要可序列化,因为它们将存储在会话中。 连接 然而,接口不扩展 Serialized,因此出现此异常。您需要使其瞬态

真正的问题实际上更大;将外部资源作为类的一个字段是一个非常糟糕的主意。您永远不应该将数据库资源作为类的字段,其寿命比资源需要保持打开的时间长。否则它们将泄漏和/或导致线程安全和事务问题(死锁等)和/或将被数据库回收并因此停止工作。

您应该始终在尽可能短的范围内打开和关闭连接(以及语句和结果集),最好在同一方法块的try-finally块内。如果您想提高连接性能,那么您应该使用连接池。

另请参阅:

You've assigned a JDBC Connection as a property of the view scoped managed bean. View scoped managed beans and all of its properties needs to be serializable as they will be stored in the session. The Connection interface does however not extend Serializable and hence this exception. You would need to make it transient.

But the real problem is actually bigger; holding external resources as a field of a class is an extremely bad idea. You should never hold DB resources as a field of a class which lives longer than the resource needs to be kept open. Otherwise they will leak away and/or will cause threadsafety and transactional problems (deadlocks and so on) and/or will be reclaimed by the DB and will thus stop working.

You should always open and close the connection (and statement and resultset) in the shortest possible scope, preferably inside a try-finally block of the very same method block. If you want to improve connecting performance, then you should be using a connection pool.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文