Professional Web Applications Themes

Persistent CFCs and Queries - Coldfusion - Advanced Techniques

I have just recently gotten into using persistent CFCs and I have read about the various read/write issues, when to use locking, as well as the best practices. However, I am still unclear on one thing. Is it a bad idea to use a <cfquery> inside of a CFC that belongs to a shared scope? To use a simple example, lets say I have a CFC called NewsManager that is stored as application.newsManager in my CF app. It has two methods, one called getArticles() and another called updateArtcile(). getArticles() runs a basic select query and just returns the query object. ...

Sponsored Links
  1. #1

    Default Persistent CFCs and Queries

    I have just recently gotten into using persistent CFCs and I have read about
    the various read/write issues, when to use locking, as well as the best
    practices. However, I am still unclear on one thing. Is it a bad idea to use a
    <cfquery> inside of a CFC that belongs to a shared scope?

    To use a simple example, lets say I have a CFC called NewsManager that is
    stored as application.newsManager in my CF app. It has two methods, one called
    getArticles() and another called updateArtcile(). getArticles() runs a basic
    select query and just returns the query object. updateArticle() would take an
    ID and some article info (text, date, etc) as arguments, and then run an UPDATE
    query to modify the article.

    Now I know the database should do all of it's own locking, plus I can use
    <cftransaction>'s to help that as well. My main question, is that if these 2
    methods are part of a CFC in a shared scope, and lets say 2 users access the
    getArticles() method concurrently, is there any chance of error? Neither method
    is writing or reading to a shared scope, just running queries on my database.
    Should I be using named <cflock>s, should I not have these queries as part of a
    CFC in a shared scope, or is what I'm doing perfectly acceptable as a best
    practice?

    Thanks,

    Justin Holzer

    Sponsored Links
    thedude500 Guest

  2. #2

    Default Re: Persistent CFCs and Queries

    I don't think any of that matters. What is your actual concern?

    Originally posted by: thedude500
    I have just recently gotten into using persistent CFCs and I have read about
    the various read/write issues, when to use locking, as well as the best
    practices. However, I am still unclear on one thing. Is it a bad idea to use a
    <cfquery> inside of a CFC that belongs to a shared scope?

    To use a simple example, lets say I have a CFC called NewsManager that is
    stored as application.newsManager in my CF app. It has two methods, one called
    getArticles() and another called updateArtcile(). getArticles() runs a basic
    select query and just returns the query object. updateArticle() would take an
    ID and some article info (text, date, etc) as arguments, and then run an UPDATE
    query to modify the article.

    Now I know the database should do all of it's own locking, plus I can use
    <cftransaction>'s to help that as well. My main question, is that if these 2
    methods are part of a CFC in a shared scope, and lets say 2 users access the
    getArticles() method concurrently, is there any chance of error? Neither method
    is writing or reading to a shared scope, just running queries on my database.
    Should I be using named <cflock>s, should I not have these queries as part of a
    CFC in a shared scope, or is what I'm doing perfectly acceptable as a best
    practice?

    Thanks,

    Justin Holzer



    Dan Guest

  3. #3

    Default Re: Persistent CFCs and Queries

    My concern is whether using a <cfquery> in a public method of a globally scoped
    CFC (in my case it's the application scope):

    1) can cause a race condition or any other type of threading issue?
    2) is considered/not considered a best practice

    Based on macromedia's reccomendation, I've decalred my query object as a
    function local variable before using <cfquery>. I just want to make sure that I
    do not need to perform any kind of locking, other than maybe using a
    <cftransaction> to lock the appropriate db tables/records. I've included a tiny
    code snippet to demonstrate basically what I'm doing. I just want to make sure
    I'm following best practices, and if not, I'd like to know what I'm doing wrong.

    Thanks,

    Justin

    [code snippet]

    <cffunction name="getArticles" returntype="query">
    <cfargument name="startDate" type="date" />
    <cfargument name="endDate" type="date" />

    <cfset var articles = "" />
    <cfquery name="articles" dsn="myDSN">
    SQL query stuff...
    </cfquery>

    <cfreturn articles />
    </cffunction>


    <cffunction name="updateArticle">
    <cfargument name="id" type="numeric" />
    <cfargument name="text" type="string" />

    <cfset var updArticle = "" />
    <cfquery name="updArticle" datasource="myDSN">
    UPDATE ...
    </cfquery>
    </cffunction>

    thedude500 Guest

  4. #4

    Default Re: Persistent CFCs and Queries

    Looks fine to me. I wouldn't even worry about the cftransaction. The only
    thing I would do differently is to have a boolean return variable in your
    update function. I would also put the query inside a cftry, and the cfcatch
    would set the return variable to false.

    I don't know if it's a best practice or not. I like to have a return value
    for all UDFs so that the syntax for calling them is the same as calling native
    cf functions.


    Originally posted by: thedude500
    My concern is whether using a <cfquery> in a public method of a globally
    scoped CFC (in my case it's the application scope):

    1) can cause a race condition or any other type of threading issue?
    2) is considered/not considered a best practice

    Based on macromedia's reccomendation, I've decalred my query object as a
    function local variable before using <cfquery>. I just want to make sure that I
    do not need to perform any kind of locking, other than maybe using a
    <cftransaction> to lock the appropriate db tables/records. I've included a tiny
    code snippet to demonstrate basically what I'm doing. I just want to make sure
    I'm following best practices, and if not, I'd like to know what I'm doing wrong.

    Thanks,

    Justin

    [code snippet]



    Dan Guest

  5. #5

    Default Re: Persistent CFCs and Queries

    Sounds good. Thanks for all the help.

    - Justin
    thedude500 Guest

Similar Threads

  1. Persistent Shared Object not being persistent
    By Milks808 in forum Macromedia Flash Flashcom
    Replies: 5
    Last Post: August 6th, 04:13 PM
  2. Replies: 0
    Last Post: December 27th, 11:43 PM
  3. Finding CFCs
    By David in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: November 11th, 01:11 PM
  4. CFCs and MX7
    By ldonner in forum Coldfusion - Getting Started
    Replies: 0
    Last Post: June 22nd, 04:27 PM
  5. Queries Of Queries Single Quote Problem
    By TimH2O in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 1st, 07:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139