I’m just thinking about this. If I have dao bean in spring and that dao bean has a protected/public property “currentSQL”. It will be value in this property “visible” for all concurent requests or not ?
Example:
@Component
public class FooDAO {
@Autowired
private DataSource dataSource;
private String currentSQL;
public void doSome()
{
currentSQL = "Foo SQL query";
}
public String getSome()
{
return currentSQL;
}
}
Is currentSQL property in that example above safe per request or not ?
it depends on how you set the scope of FooDAO bean.
for every access (request) on fooDAO spring creates a new instance of FooDAO. Then it can be considered as Thread-safe, as long as your local threads don’t use the same instance of FooDAO. If it is the case, you have to handle thread access with “public synchronized void doSome()”
if your bean Singleton, then it’s not thread-safe. The singleton instance is shared among your application. If this is the case, then you have to synchronize the method.