I wrote a wrapper around database queries and need to access it from different threads. Therefore my application creates exactly one instance of that helper class and returns it through a getter.
DbConnection dbc = app.getDatabaseConnection();
synchronized (dbc) {
dbc.doSomething();
}
Is this code safe? As stated here it should work although synchronizing on a local variable. Is this correct as long as the object instance is guaranteed to be the same?
Is it a better approach to make all affected instance methods of DbConnection synchronized?