Assigning Unique Values

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Assigning Unique Values

Carl Ketterling
I'm using Lasso 9, Mac OS X v10.6, and FileMaker 11.

I have a database of items (one per record), and I want to find one and
mark it as used.  I have a status field that I search to find the next
available record.  When I've found it, I update a field in the record as
used.  The question I have is, if two sessions do this at the same time,
they would both get the same item record.  Does someone here have a way
to work around this problem?  Is there a way to make Lasso block
database access between the search and the update?

Carl

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

stevepiercy
The Knop framework has an implementation of record locking on
database objects.  Suggest looking at it for examples.

https://github.com/knop-project/knop/blob/master/knop9/knoplibs/knop_database.lasso

It's pretty thorough in that it also handles clearing of locks
that have grown stale and integrates with authenticated users.

--steve


On 8/6/12 at 2:35 PM, [hidden email] (Carl Ketterling) pronounced:

>I'm using Lasso 9, Mac OS X v10.6, and FileMaker 11.
>
>I have a database of items (one per record), and I want to find one and
>mark it as used.  I have a status field that I search to find the next
>available record.  When I've found it, I update a field in the record as
>used.  The question I have is, if two sessions do this at the same time,
>they would both get the same item record.  Does someone here have a way
>to work around this problem?  Is there a way to make Lasso block
>database access between the search and the update?
>
>Carl
>
>#############################################################
>This message is sent to you because you are subscribed to
>the mailing list Lasso
>[hidden email]
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- --
Steve Piercy               Web Site Builder              
Soquel, CA
<[hidden email]>                  <http://www.StevePiercy.com/>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Jolle Carlestam-3
In reply to this post by Carl Ketterling
Using Knop as Steve suggests is one way.
If not then I would explore using a thread object. The thread will only execute one call at a time so it will ensure that you avoid collisions.

Something like this:
define getnextnumber => thread {

        public onCreate() => {
                // search for next number using a plain inline
                // store it in a local
                local(nextnumber = field('nextnumber'))
                // set the found rec to 'used' using a plain inline
                return #nextnumber
        }
}

Call it:
getnextnumber

HDB
Jolle

6 aug 2012 kl. 21:35 skrev Carl Ketterling:

> I'm using Lasso 9, Mac OS X v10.6, and FileMaker 11.
>
> I have a database of items (one per record), and I want to find one and
> mark it as used.  I have a status field that I search to find the next
> available record.  When I've found it, I update a field in the record as
> used.  The question I have is, if two sessions do this at the same time,
> they would both get the same item record.  Does someone here have a way
> to work around this problem?  Is there a way to make Lasso block
> database access between the search and the update?
>
> Carl
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>
>
>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Carl Ketterling
Thanks Jolle,

I decided to test this because of how simple it looked, and I found the
Lasso threading guide as well:
http://www.lassosoft.com/Language-Guide-Threading

Using onCreate, I could not get any values to return.  When I used the
example you provided, the output was "getnextnumber" instead of the
value that I was returning.

I tried defining a thread like the counter_thread object in the Language
Guide: Threading.  While calling the thread worked and the correct
information was returned, the problem wasn't resolved.

The heart of my threads were like you've described except that I added a
sleep to be able to run the same page twice:
> // search for next number using a plain inline
> // store it in a local
> local(nextnumber = field('nextnumber'))
   sleep (5000);
> // set the found rec to 'used' using a plain inline
> return #nextnumber

Using onCreate, two records were assigned to two different pages (but I
couldn't get the information into a variable).

Using something like "counter_thread->advanceBy" in the Language Guide,
the data was returned, but the problem existed where the same record was
given to two different users.

Note: I was testing with the same browser and two windows, and also two
different browsers on the same computer.  I've not tested with two
different computers (which is what would exist in the real world).

Barring any suggestions for modifying my test setup, I'll read the Knop
documentation I've downloaded.

Carl



In response to this text from Jolle Carlestam
([hidden email]) sent on Tuesday, August 7, 2012 at 6:04 AM
(+0000):

>Using Knop as Steve suggests is one way.
>If not then I would explore using a thread object. The thread will only
>execute one call at a time so it will ensure that you avoid collisions.
>
>Something like this:
>define getnextnumber => thread {
>
> public onCreate() => {
> // search for next number using a plain inline
> // store it in a local
> local(nextnumber = field('nextnumber'))
> // set the found rec to 'used' using a plain inline
> return #nextnumber
> }
>}
>
>Call it:
>getnextnumber
>
>HDB
>Jolle
>
>6 aug 2012 kl. 21:35 skrev Carl Ketterling:
>
>> I'm using Lasso 9, Mac OS X v10.6, and FileMaker 11.
>>
>> I have a database of items (one per record), and I want to find one and
>> mark it as used.  I have a status field that I search to find the next
>> available record.  When I've found it, I update a field in the record as
>> used.  The question I have is, if two sessions do this at the same time,
>> they would both get the same item record.  Does someone here have a way
>> to work around this problem?  Is there a way to make Lasso block
>> database access between the search and the update?
>>
>> Carl
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>>  the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>>
>>
>
>#############################################################
>This message is sent to you because you are subscribed to
>  the mailing list Lasso
>[hidden email]
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Jolle Carlestam-3
7 aug 2012 kl. 18:26 skrev Carl Ketterling:

> I decided to test this because of how simple it looked

Hi Carl!

Was a bit too simple. Sorry, my fault.

Here are the caveats. I put the code inside the oncreate. That was wrong. A thread object is only created once unlike types. So it will only call oncreate once.
And, you have to put the code defining the thread somewhere were it only runs once. Like in your library or startup directory. Or you can use a conditional so you don't define the thread more than once. Here's an example. This time using code that I've actually tested:

if(not lasso_tagexists('nextnumber')) => {
        define nextnumber => thread {

                data public counter::integer = 1

                public get() => {
                        local(counter = integer(.'counter'))
                        .'counter' += 1
                        return #counter
                }


        }
}
br
nextnumber -> get

HDB
Jolle
#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Jolle Carlestam-3
7 aug 2012 kl. 21:14 skrev Jolle Carlestam:

Let me explain the steps a bit more

> if(not lasso_tagexists('nextnumber')) => {
This conditional will make sure that the thread definition only run once.
The "not [condition]" is the same as using explanation mark but visually more appealing. Could just as well be if(!lasso_tagexists('nextnumber')) or if(lasso_tagexists('nextnumber') == false)

> define nextnumber => thread {
Starting the thread definition. Note that unlike types or methods redefining a thread will not replace the previous definition. Only way to get rid of a thread that's been defined is to restart the instance. Not really convenient but just something we have to deal with.

> data public counter::integer = 1
Here's where we store the persistent value. Will be 1 when the thread object is created.

> public get() => {
This would be the method we call to hand us the next value.

> local(counter = integer(.'counter'))
We set the local to integer(.'counter') to get rid of the reference. Had we done local(counter = .'counter') the following change would have affected the local as well.

> .'counter' += 1
Bump the persistent data by one.

> return #counter
Send back the present counter value.


That's it.

HDB
Jolle
#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Jolle Carlestam-3
7 aug 2012 kl. 21:33 skrev Jolle Carlestam:

>> if(not lasso_tagexists('nextnumber')) => {
>

And for the record. I don't recommend this approach. I think best practices is to put all your custom types, methods and threads in either LassoLibraries, LassoStartup or as part of a LassoApp in LassoApps.

My favorite since LDC is the LassoApp approach. Thanks Jono for the pointers!!

HDB
Jolle
#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Jonathan Guthrie-3
You're welcome :)
FWIW as you're developing it's not an issue to have them in LassoApps because you can use a directory based LassoApp, which means you can still work on them, change them etc.
If you change one and want to load the change, just hit the file directly in your browser, i.e. http://yourhost/lasso9/yourlassoapp/path/to/file.lasso

On 2012-08-07, at 3:41 PM, Jolle Carlestam <[hidden email]> wrote:

> My favorite since LDC is the LassoApp approach. Thanks Jono for the pointers!!

Jono

----------------------------
Jonathan Guthrie
[hidden email]
LassoSoft Inc.
+1 888-286-7753 ext 708

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Jolle Carlestam-3
7 aug 2012 kl. 22:20 skrev Jonathan Guthrie:

> FWIW as you're developing it's not an issue to have them in LassoApps because you can use a directory based LassoApp, which means you can still work on them, change them etc.
> If you change one and want to load the change, just hit the file directly in your browser, i.e. http://yourhost/lasso9/yourlassoapp/path/to/file.lasso

Is what I do. Although I took it a step further. In my LassoApp I have an index[html].lasso file. In it I have a nicely presented form with a checkbox list presenting all my type,thread and method files. Allowing me to tick the ones I want reloading and doing it for me using an ajax call. Very convenient. There's also a help section in it so that I have access to a reference in case I or any colleague forget how to call the different tags.

I've implemented this for the Open Source Knop Framework and plan to have it available for public access as soon as I and Steve have worked out how to present the reference in best possible way.

HDB
Jolle
#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Assigning Unique Values

Bil Corry-3
In reply to this post by Carl Ketterling
If it were MySQL, I'd have the page create a unique ID, then UPDATE based on the search criteria with LIMIT 1 and set the 'used' flag to the unique ID, then perform a search using the unique ID to find the details of the row selected.

So rather than find and update, instead update then find.

Maybe you can do the same in Filemaker?


- Bil

On Aug 6, 2012, at 12:35 PM, "Carl Ketterling" <[hidden email]> wrote:

> I'm using Lasso 9, Mac OS X v10.6, and FileMaker 11.
>
> I have a database of items (one per record), and I want to find one and
> mark it as used.  I have a status field that I search to find the next
> available record.  When I've found it, I update a field in the record as
> used.  The question I have is, if two sessions do this at the same time,
> they would both get the same item record.  Does someone here have a way
> to work around this problem?  Is there a way to make Lasso block
> database access between the search and the update?
>
> Carl
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>
#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>