# Persistent objects

**URL:** <https://www.ruby-forum.com/t/persistent-objects/185857>\
**Category:** Rails\
**Created:** [March 27, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857 "2010-03-27T16:45:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [March 27, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857/1 "2010-03-27T16:45:16Z")

</div>

Is there a way to create an object that is a singleton (one instance  
per user session) and is kept as long as the user session exists? The  
object handles a connection to a remote server that is very heavy to  
establish so I would like to reuse the same object in following  
requests.

I have tried class variables and global variables but both are  
discarded at the request’s end.

Any suggestions are very welcome.

Regards

Erik

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [March 27, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857/2 "2010-03-27T17:54:19Z")

</div>

On 27 March 2010 15:44, Erik L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to create an object that is a singleton (one instance  
> per user session) and is kept as long as the user session exists? The  
> object handles a connection to a remote server that is very heavy to  
> establish so I would like to reuse the same object in following  
> requests.

You have answered your own question, put it in the session. If it is  
a large object then put it in the db and reference it from the  
session.

Colin

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [March 27, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857/3 "2010-03-27T18:34:41Z")

</div>

Hi Colin and thanks for the suggestion. But the problem is more  
intricate than this, I am afraid. The reason I want to keep the object  
is that it connects via sockets to a pool of servers (at least 4 of  
them). When the object is marshaled, either for storage in the session  
or in the db these connections are lost. Maybe there is another  
solution but as I understand it the only way to keep these connections  
is to keep the object itself, not a serialized copy.

Extremely grateful for any further input.

Regards

Erik

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [March 27, 2010, 7:04pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857/4 "2010-03-27T19:04:32Z")

</div>

On Mar 27, 5:33 pm, Erik L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Colin and thanks for the suggestion. But the problem is more  
> intricate than this, I am afraid. The reason I want to keep the object  
> is that it connects via sockets to a pool of servers (at least 4 of  
> them). When the object is marshaled, either for storage in the session  
> or in the db these connections are lost. Maybe there is another  
> solution but as I understand it the only way to keep these connections  
> is to keep the object itself, not a serialized copy.

I suspect class variables etc are only vanishing because you are in  
development mode. In production mode they will persist.

Fred

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [March 27, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857/5 "2010-03-27T20:48:44Z")

</div>

On 27 March 2010 18:13, Erik L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Excellent, you are right. I thought I tried that but I have tried many  
> combinations so I must have done it for something that was not  
> persistent in itself. @@variable worked when in production mode. You  
> made me a very happy camper.

As a matter of interest how are you deciding when to release the  
connection?

Also don’t forget that if you ever went to multiple servers to serve  
your app that successive requests from a user may not go to the same  
server.

Colin

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [March 27, 2010, 7:14pm UTC](https://www.ruby-forum.com/t/persistent-objects/185857/6 "2010-03-27T19:14:29Z")

</div>

Excellent, you are right. I thought I tried that but I have tried many  
combinations so I must have done it for something that was not  
persistent in itself. @@variable worked when in production mode. You  
made me a very happy camper.

Thanks.

Erik
