Charles Robertson
11/02/2022, 9:17 AMfunction list(rc){
rc.page = 1;
variables.blogService.page(rc.page);
rc.data = variables.blogService.list();
}
blogService
component accessors = true{
variables.blogs = {};
// constructor etc…
function page(page){
// build variables.blogs struct from DB query
}
function list(){
return variables.blogs;
}
}
So, essentially, the blogService
page method uses a DB query to populate:
variables.blogs
And then I immediately return this variable, in the list()
method.
What worries me, is that if there are two different requests like:
REQUEST 1:
rc.page = 1;
variables.blogService.page(rc.page);
rc.data = variables.blogService.list();
REQUEST 2:
rc.page = 2;
variables.blogService.page(rc.page);
rc.data = variables.blogService.list();
The list data might get mixed up?
My question:
Are the two method calls, atomic, in the blogController
, per request?
I know that Coldfusion is multi-threaded, so requests are dealth with, in parallel.
I am worried that the following might happen:
REQUEST 1 rc.page = 1;
REQUEST 1 variables.blogService.page(rc.page);
REQUEST 2 rc.page = 2;
REQUEST 2 variables.blogService.page(rc.page);
REQUEST 1 rc.data = variables.blogService.list();
REQUEST 2 rc.data = variables.blogService.list();
Essentially, the user responsible for REQUEST 1 will receive the REQUEST 2 data.
I feel that data is safe, within a singleton, as long as methods are UDFs [functional/stateless methods], but when there is stateful data, in a singleton, then the alarm bells start ringing.
I am not sure why I decided to create a component variables variable, but I think I found it in an FW1 example.
The three potential solutions I have, are:
1) Make services instances, not singletons [remove from the application scope] CANNOT DO THIS AS FW1 THROWS AN ERROR
2) Lock the two method calls, like:
cflock (name="listBlogService", type="exclusive", timeout="30") {
variables.blogService.page(rc.page);
rc.data = variables.blogService.list();
}
3) Remove the blogService
list()
method and return the list from the page()
method. This would turn the page()
method into a UDF, removing references to state.
I am veering towards option 3?websolete
11/02/2022, 12:41 PMwebsolete
11/02/2022, 12:42 PMmjclemente
11/02/2022, 1:25 PMrc.page = 1
var pageBean = variables.beanFactory.getBean("pageBean");
pageBean.setId(rc.page);
rc.data = pageBean.list();
//within pageBean
list(){
return variables.pageService.list(pageId = variables.id );
}
That code could be cleaned up, obviously, but it's a rough ideaCharles Robertson
11/02/2022, 3:47 PMCharles Robertson
11/02/2022, 3:54 PMvariables
approach from an example FW1 app, a long time ago. But, I think I misunderstood the intention of the example.
I think the original intention was to build up a Struct of blog beans, so that data could be cached, rather than having to fetch it from the DB, every time. Over time, more blog beans would get added to the Struct by many different users etc.
But, my objective is completely different. I want to create small pagination lists, so the previous objective is completely inappropriate.
I will use OPTION 3, and return the list data directly from the page()
method.