如何解決 singleton can't be dumped

問題描述:
一場堆棧如下
[code]TypeError (singleton can't be dumped):
D:/dev/InstantRails/ruby/lib/ruby/1.8/pstore.rb:349:in `dump'
D:/dev/InstantRails/ruby/lib/ruby/1.8/pstore.rb:349:in `dump'
D:/dev/InstantRails/ruby/lib/ruby/1.8/pstore.rb:330:in `transaction'
D:/dev/InstantRails/ruby/lib/ruby/1.8/cgi/session/pstore.rb:81:in `update'
D:/dev/InstantRails/ruby/lib/ruby/1.8/cgi/session/pstore.rb:88:in `close'
D:/dev/InstantRails/ruby/lib/ruby/1.8/cgi/session.rb:324:in `close'
D:/dev/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/base.rb:1190:in `close_session'
D:/dev/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/base.rb:1234:in `process_cleanup_without_flash
D:/dev/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/flash.rb:146:in `process_cleanup_without_filte
D:/dev/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/filters.rb:761:in `process_cleanup_without_ses[/code]

該問題的原因是,一些singleton對象不能放到session中。這裏的singleton對象指的是沒有辦法store 或者persistent的對象,比如:上傳的文件,這個對這個對象進行persistent是沒有意義的,因爲該文件所指向的文件源在網絡上、在瀏覽器所在的機器上。
注意,有兩種方式會引發這個異常,1、你將一個singleton對象直接放到session中 2、你放到session的對象引用了一個singleton對象。
比如,你將People類的一個事例,放到了session中,該類中引入了一個module ImageSystem,該module專門處理文件上傳的。不巧的是,該module的設計者將一個臨時文件 tempfile當成了一個instance 變量,於是異常一定會產生。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章