I have class "RequestContext" which has scope request. This class has atribute listOfItem.
now I have class MyMapper where I need to use this list. Now when I want to listOfItems I always call context.getListOfItem() but problem is that I have a lot of private method where I need to repeat this a lot of times. It is ok when I define this atribute in constructor ? It is thread safe ?:
public abstract class MyMapper{
#Autowired
protected RequestContext context;
private final List<String> listOfItem;
public MyMapper() {
this.listOfItem = context.getListOfItem(); // is this thread safe and ok ?
}
public Object map(Object entity){
}
}
Yes, that is thread safe as long as it is declared as a prototype scope bean, and you need to create an init() method that is called by Spring:
#PostConstruct
public void init() {
listOfItem = context.getListOfItem();
}
The RequestContext is only accessible from single thread (the one allocated to handle the request), the constructor is not re-entrant by nature of the creation of the object just before it's call.
Be careful not to confuse this with the listOfItem somehow being safe from reentrancy problems though, just because it is locked down in the MyMapper object does not stop it from being shared by a getter, if one were available (there isn't in your case). I also see that it's an abstract class, but because the listOfItem is private, subclasses will not have access to it. Any leaked reference of that List could be manipulated by concurrent threads were there to be any copies made of the reference (since Lists are mutable in Java).
As this safety is your intent, create a unit test that checks the visibility of the field and fails if accessing the field via reflection does not throw the appropriate exception. You may also want to comment the field with your own internal marker annotation to indicate the field is thread safe. This helps with documentation, and as an annotation, potential future automation (such as a test base that could look for all such annotations and automatically run the reflection test).
It looks very clean! Keep up the good work.
Related
I have an Ingestion class that exposes a single method ingest. This method processes each section of a passed in form (section 1, section 2, etc, etc).
I have private methods for each section, saving the entity as it processes through. I'm aware that #Transactional has no effect on private methods, however I do not want to expose these methods but would like to use the functionality that #Transactional provides.
I'm looking to make sure each section completes in its own Transaction; I could do this through 'AspectJ' (as other SO answers have suggested) instead of Spring's out the box implementation, but I am trying to avoid due to the system wide changes it would cause.
Any thoughts on another approach?
The pseudo code provided below gives a general idea on the structure of the class:
public Class Ingestion {
// Autowired Repo's
...
...
#Transactional
public void ingest(Form form){
this.processSection1(form);
this.processSection2(form);
this.processSection3(form);
}
#Transactional
private void processSection1(Form form){
// do specific section 1 logic
section1Repo.save(form);
}
#Transactional
private void processSection2(Form form){
// do specific section2 logic
section2Repo.save(form);
}
#Transactional
private void processSection3(Form form){
// do specific section3 logic
section3Repo.save(form);
}
}
=========================================================================
This is not a duplicate question as marked in the comments. I know #Transactional doesnt work on private methods. My question is more along the lines of 'how do we get around this Spring AOP issue without having to use AspectJ'
The reason this doesn't work is that annotations like #Transactional add additional functionality that is intercepted by Spring's proxy object that wraps the actual object. But when you call a private method on an object with the this keyword, you're going straight to the real object and bypassing the proxy.
One way to solve this is to #Autowire the object into itself, and make the transactional calls via that autowired variable. You can still access private methods that way, and the call will be to a Spring-managed proxy instead of the bare object.
You may extract these three processing methods in another class, make them public, but set the class constructor access level to package-local (but not private, since Spring can't proxy classes with private constructors), so no classes from other packages could access these methods just because they are not able to instantiate their class. It doesn't hide these methods completely, but may fit your needs. This trick can be done with an inner class as well (note that it must be declared with package-local access).
To completely hide these methods, you may make use of declarative transaction management by injecting TransactionTemplate bean and using its execute method in private methods. This feature comes out-of-the-box. See more here.
Also, take note that for creating new transaction on executing method B from method A, method B must be declared #Transactional with propagation type REQUIRES_NEW. Otherwise, any nested methods will be invoked in the same transaction started by initial calling method.
Should Guice Providers be annotated with #Singleton? My justification: if the Provider is providing an object to other Singleton classes and the object itself is relatively expensive to create, then wouldn't it make sense to use a Singleton Provider that constructs the expensive object in its #Inject-marked constructor, store it as a member and just return that already-saved global variable in the getter? Something like this:
#Singleton
public class MyProvider extends Provider<ExpensiveObject> {
private ExpensiveObject obj;
#Inject
public MyProvider() {
/* Create the expensive object here, set it to this.obj */
}
#Override
public ExpensiveObject get() {
return obj;
}
}
Update
Let me clarify a little bit more here. This is not about whether I should be using #Singleton or .in(Singleton.class). This has to do more with the "caching" of the created object.
Let's say that object creation required multiple RPCs to complete, such as deserializing JSON or making HTTP requests. This could take quite some time. If I am going to use this Provider to inject into classes multiple times, then doesn't it make sense to only create such an object once?
Also note that I must be able to use a Provider because I need to be able to inject into the Provider.
If your question is whether you should create scoped provider bindings, or if you should cache instances in your providers manually, then really, do not try to be smarter than Guice :) You really do not want to do anything more than just create your expensive object in get() method. Simple test case:
public class MyProvider implements Provider<String> {
public String get() {
System.out.println("Called MyProvider.get()");
return "abcd";
}
}
public class MyModule extends AbstractModule {
protected void configure() {
bind(String.class).toProvider(MyProvider.class).in(Singleton.class);
}
}
Injector injector = Guice.createInjector(new MyModule());
String abcd1 = injector.getInstance(String.class); // Prints "Called MyProvider.get()
String abcd2 = injector.getInstance(String.class); // Prints nothing!
// Or, if you want, comment out above two lines and try the following:
Provider<String> abcdProvider = injector.getProvider(String.class);
abcdProvider.get(); // Prints "Called MyProvider.get()"
abcdProvider.get(); // Prints nothing
You see, because the message was printed only once, MyProvider.get() method was called only once too, exactly because String is bound in singleton scope.
The key concept to understand here is that providers and bindings are not separate entities. With every binding there is an associated provider (when you create plain bindings with to(), an implicit provider is created for you). This can easily be observed from getProvider() method signature - it accepts Class<T> or Key<T> for actual class you want to get, not for the provider you have bound. When you create a binding to specific provider, you do not configure this provider, you configure the binding. Guice is smart enough to take scopes into account even if you use explicit providers, so you just do not need to reinvent the wheel and roll out your own singleton.
If your question is specifically about usage of #Singleton annotation (as opposed to bind() DSL), then I don't know whether its presence on provider class gives any effect, but given that you should use bind().toProvider() to bind to this provider anyway, I don't think that it really matters. Just use in() method, it will certainly work.
Please beware that there is a major difference between binding your provider to the Singleton scope by using .in(Singleton.class) and using the #Singleton annotation on your provider class.
In the first case the get() method is only called once and the result will be stored in the Singleton scope.
In the second case the provider instance is just created once but the get() method is called for each injection point in your application.
If you use this approach it would be wise to manually cache your expensive object. But there is no point in doing so, really. Just use the first approach and you are fine.
You could even combine the two strategies, e.g. by annotating the provider with #Singleton and bind the provider result to request scope by using .in(RequestScoped.class). Without the annotation your provider will be instanciated for each request, which might matter if it stores stateful data.
This is just for clarification as some reader may stumble upon your question and may think the two approaches are sematically equal.
Yes, in principle, but then in this instance, you could do away with the provider altogether and just create ExpensiveObject as an eager singleton. It'll only be instantiated once when the injector is created, and that single instance will get injected everywhere it's required.
I have a class as follows
class Myclass{
//no instance variables
static boolean validate(MyObj oj){
//impl
}
}
Now if 2 thread calls static method Myclass.validate(param) with different parameters at the same time ,will it work correctly? If yes/no, how?
Is my approach correct? I want to put some validation logic or some custom conversion utility in such static methods.
1- Call is safe since the obj parameter is local to the method. However ensure that
The Obj is not shared by different threads. If it is then sate is not
modified. (should be immutable)
The object re3fernce is not passed to any alien method, which
might be not thread safe.
You can mark the parameter as final.
2- Its OK to have static methods for classes which don't have any state.
If you don't have any instance variables, you have a utility class.
public enum Utility {;
public static boolean validate(MyObj obj) ....
}
However a better approach is to move the method to the first parameter type, if you can.
public class MyObj {
public boolean validate() ....
}
Provided the arguments are not shared, two threads can call the same method without thread safety issue.
If this is for validation, or conversion a utility class may be a better choice if you want more than one way to validate or convert the MyObj type.
You use case is a perfect fit for an Utility class and making the utility class methods static will help you use the class without instantiating it (Hence avoiding object littering and GC overhead ).
Thread safety is not an issue here as you are using any shared variable (class variables). So you are safe that way.
So both #1 and 2# will work.
Yes, it will work correctly. Parameters passed to a method lives in
a memory area called the "stack" and each thread will have it's own
stack.
I would say yes
I have a manager as Spring wired bean. I believe every bean defined for spring by default is wired as singleton. I have some methods in this bean which I need to synchronize.
How should I be doing that then --
void zzz() {
synchronized (this) {
...
}
}
or
void zzz() {
synchronized (MyClass.class) {
...
}
}
?
The main difference in the two is that in the first case, the the instance of the class as the monitor and the second one uses the Class as the monitor.
The first one is probably the way to go in your case because, in the near future if you decide to have many instances of your class, their methods will be synchronized on the respective instances. As opposed to if you use a Class as a monitor, if one thread is calling a synchronized method on one instance, no other threads will be able to call methods (those that are synchronized) on any instances of the same class.
Unless you're accessing mutable static class variables (potentially nasty to begin with), the first is the appropriate way to synchronize.
Understand that while Spring is only creating one instance of a singleton bean and using it for anybody who has a dependency on a bean of that type, the singleton is not a static entity. There is no compiler constraint preventing you from instantiating that class yourself outside the Spring context. It's simply the only instance because Spring knows not to make more of them... not because it can't be done. The point I'm trying to make here is that it's iffy bordering on incorrect to draw a parallel between class-level data and the singleton's data.
In corollary, synchronization should occur on the narrowest scope possible. In your case, that means synchronize on the object instance containing the shared data rather than on the wider scope of the entire class.
Is the following code threadsafe ?
public static Entity getInstance(){
//the constructor below is a default one.
return new Entity();
}
Assuming the constructor itself is thread-safe, that's fine.
It would be very unusual for a constructor not to be thread-safe, but possible... even if it's calling the default auto-generated constructor for Entity, the base constructor may not be thread-safe. I'm not saying it's likely, just possible :)
Basically there's no magic thread-safety applied to static methods or instance methods or constructors. They can all be called on multiple threads concurrently unless synchronization is applied. If they don't fetch or change any shared data, they will generally be safe - if they do access shared data, you need to be more careful. (If the shared data is immutable or only read, that's generally okay - but if one of the threads will be mutating it, you need to be really careful.)
Only static initializers (initialization expressions for static variables and static { ... } blocks directly within a class) have special treatment - the VM makes sure they're executed once and only once, blocking other threads which are waiting for the type to be initialized.
It depends on the details of the Entity constructor. If the Entity constructor modifies shared data, then it is not.
It's probably thread safe, but what's the point? If you're just using a factory method to redirect to the default constructor then why not use the constructor in the first place? So the question is: what are you trying to achieve? The name getInstance() suggests a singleton (at least that's common practice), but you clearly don't have a singleton there. If you do want a singleton, use a static inner holder class like this:
public class Singleton {
private Singleton() {
}
public static Singleton getInstance() {
return InstanceHolder.INSTANCE;
}
private static final class InstanceHolder {
public static final Singleton INSTANCE = new Singleton();
}
}
but if you don't, why bother with such a factory method, as you're not adding any value (method name semantics, object pooling, synchronization etc) through it
Thread safety is about access to shared data between different threads. The code in your example doesn't access shared data by itself, but whether it's thread-safe depends on whether the constructor accesses data that could be shared between different threads.
There are a lot of subtle and hard issues to deal with with regard to concurrent programming. If you want to learn about thread safety and concurrent programming in Java, then I highly recommend the book Java Concurrency in Practice by Brian Goetz.
Multiple threads could call this method and each one will get an unique instance of 'Entity'. So this method 'per se' is thread safe. But if there is code in the constructor or in one of the super constructors that is not thread safe you might have a safety problem anyhow.