Caution The inject annotations are done in comments as ColdFusion 9 has a bug when adding annotations on scripted arguments.
onDiComplete()method. With this normal approach, the wiring of dependencies and
onDiComplete()have the potential of mixups or missing dependencies due to concurrency. This is a normal side-effect and risk that developers take due that Java makes no guarantees that any thread other than the one that set its dependencies will see the dependencies. The memory between threads is not final or immutable so properties can enter an altered state.
"The subtle reason has to do with the way Java Virtual Machines (JVM) are designed to manage threads. Threads may keep local, cached copies of non-volatile fields that can quickly get out of sync with one another unless they are synchronized correctly." From Dependency Injection by Dhanji R. Prasanna
Note This side effect of concurrency will only occur on objects that are singletons or persisted in scopes like session, server, application, server or cachebox. It does not affect transient or request scoped objects.
ThreadSafeannotation or our binder
threadSafe()tagging method. So if we wanted to make the last example thread safe for property and setter wiring then we would do the following:
Note All objects are marked as non thread safe for dependency wiring by default in order to allow for circular dependencies. Please note that if you mark an object as
threadSafe, then it will not be able to support circular dependencies unless it uses WireBox providers. ( See Providers Section )
threadSafeannotation and binder tagging property will allow for these objects to be completely locked and synchronized for object creation, wiring and
onDiComplete(). However, circular dependencies will now fail as persistence cannot be guaranteed for the setter or property dependencies. However, since WireBox is so awesome, you can still use circular dependencies by wiring instead our object providers. (Please see providers section). In conclusion, constructing and designing a CFC that is thread safe is often a very arduous process. It is also very difficult to test and recreate threading issues in your objects and applications. So don't feel bad, as even the best of us can get into some nasty wormholes when dealing with concurrency and thread safety. However, always try to design for as much concurrency as possible and test test test!