0

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.

  • 1
    *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* that's the main purpose of managed beans, so if you're using the managed beans for something else that belongs to your model like business logic classes then you're not learning JSF. – Luiggi Mendoza Jul 02 '13 at 19:44
  • I am just SO confused about not being able to use "one tool" for everything. ;) Part of my application consists of managedbeans and the other part is not. –  Jul 02 '13 at 19:45
  • Looks like you need to check http://stackoverflow.com/q/5104094/1065197. I would like to highlight this part: **M - Business domain/Service layer (e.g. EJB/JPA/DAO)**. – Luiggi Mendoza Jul 02 '13 at 19:46
  • To enable Spring dependency injection, check [this answer](http://stackoverflow.com/a/17278608/1065197). But if you're running your application over a Java EE compliant application server like JBoss 7 or GlassFish 3 or 4, use EJB instead of Spring. – Luiggi Mendoza Jul 02 '13 at 19:48
  • I think here the Singleton is best approach, but beware of connection object, make sure every requests just work with one state of the connection, otherwise you need locking –  Jul 02 '13 at 19:54
  • @user2511414 in fact, singleton is not the best approach in this kind of applications. It would be better manually creating the DAO or even better: inject an EJB that already get the database connection by a `@Resource DataSource ds` (of course, this in case you're using JDBc instead of a framework that handles the db connectivity for you like Hibernate or JPA). – Luiggi Mendoza Jul 02 '13 at 19:57
  • @LuiggiMendoza I'm completely agree with you, but EJBs are not managed directly by user, if user want to have implicit manage of a statefull component, it dependents –  Jul 02 '13 at 19:59
  • @user2511414 if I would want to have explicit manage of the component, I would opt for creating the beans myself =\ – Luiggi Mendoza Jul 02 '13 at 20:01
  • @LuiggiMendoza I don't mention about the way of getting the connection, JDBC, JPA, ..., it up to dev –  Jul 02 '13 at 20:01

0 Answers0