• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to instantiate a persistent Singleton

Guest
Jun 25, 2007 Jun 25, 2007

Copy link to clipboard

Copied

I have a data access component for accessing non-relational data stores, and in order to make it useful, I need to enable pooling. What would work best would be if I could instantiate a singleton class when starting coldfusion, but I have no idea how to do that. Is there a way to instantiate a class that persists through the life of the server, or even through the life of a session? The latter would be more of a work-around but I could probably live with it.

Any help would be appreciated.
TOPICS
Advanced techniques

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jun 26, 2007 Jun 26, 2007
OK if you are using Application.cfm then the following:-

If (NOT structKeyExists(application, "myObj")) {
application.myObj = createObject("component", "path.to.cfc");
{

if you are using Application.cfc then the following in then onApplicationStart() function:-

application.myObj = createObject("component", "path.to.cfc");

If you aren't using either then you should be!!!!

Votes

Translate

Translate
Engaged ,
Jun 25, 2007 Jun 25, 2007

Copy link to clipboard

Copied

You can instantiate the object in application scope. I recommend not instantiating objects in session scope as it can get out of hand and cause memory problems.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 26, 2007 Jun 26, 2007

Copy link to clipboard

Copied

Stressed_Simon's recommendation is a good one. Caching a stateless "singleton" class in the application scope is the best way to accomplish this. We do this quite a bit for our DAO's.

Though not strictly required, you can streamline access to your application-scoped singleton CFCs using a framework like ColdSpring:
http://www.coldspringframework.org

Best of luck.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 26, 2007 Jun 26, 2007

Copy link to clipboard

Copied

Can someone point me at sample code to do this? I'm new to cfscript and while I found lots of talk about variable scope, I found precious few useful examples of how to create a variable of server or application scope and then reference it.

Thanks,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 26, 2007 Jun 26, 2007

Copy link to clipboard

Copied

application.myObj = createObject("component", "path.to.cfc");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 26, 2007 Jun 26, 2007

Copy link to clipboard

Copied

Hmmm. I'm not getting something here. Is there a special place that I put this? If I put this in script on my page, doesn't it create a new instance of my object every time? I really wanted just one instance of the object. Guaranteed one instance, but only one.

I may have found what I'm looking for here:
http://www.webtricks.com/sourcecode/code.cfm?CodeID=25
It looks a bit complex, but I think it might work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 26, 2007 Jun 26, 2007

Copy link to clipboard

Copied

OK if you are using Application.cfm then the following:-

If (NOT structKeyExists(application, "myObj")) {
application.myObj = createObject("component", "path.to.cfc");
{

if you are using Application.cfc then the following in then onApplicationStart() function:-

application.myObj = createObject("component", "path.to.cfc");

If you aren't using either then you should be!!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 26, 2007 Jun 26, 2007

Copy link to clipboard

Copied

LATEST
Thanks, I think that's what I was missing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation