java.io.NotSerializedException: org.postgresql.jdbc4.Jdbc4Connection
我有一个具有视图范围的托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将 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. TheConnection
interface does however not extendSerializable
and hence this exception. You would need to make ittransient
.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: