I am completely new to Java Web development. Currently I am using JSF and with JSF it is very hard and confusing to get some Managed Bean inside a method. So I use Singletons instead of ManagedBeans. In fact, I only use ManagedBeans for the part of the application related to view - only when I need to call methods or get data via expression language.
This is how I use it now:
public void getSomeData(){
Connection connection = ConnectionFactory.getConnection();
//Retrieve the data from database
connection.close();
}
This how I DO NOT want to use it:
public class SomeDAO{
@ManagedProperty(....)
private Connection connection;
public void getSomeData(){
Connection connection = ConnectionFactory.getConnection();
//Retrieve the data from database
connection.close();
}
}
As you can see, when I only want my connection to be taken from the pool inside a method and after this method is finished, I want it to be returned back to the pool by caliing conection.close(). I don't want to create a class property and inject database connection via JSF ManagedProperty annotation. I want my DAO objects to be created only once, but consume connections only when their methods are called.
I find this almost impossible to achieve with JSF. Did I get something wrong ? Will Spring dependency injection help me with this issue ?
Please tell me your opinions.