Manage connection database with jdbc in jsf

Hello please help me with this:

I need know as manage connection in jsf. I use JSF 1.1 and use JDBC.

My project has a structure MVC. My connection is gets from a Datasource.

Question 1: Should I create in a package my connection factory class. Question 2: Where is correct managed commit and rolback in dao, businesslogig, backingbean package? Question 4: Where is correct managed open and close connection in dao, businesslogig, backingbean package?

My connection factory class is this below

public class ConexionDB {
private static Connection connection = null;
private static  DataSource dataSource=null;

private static void setupDataSource() throws IOException, NamingException {
    Propiedades propiedades = new Propiedades();
    Context  initialContext = new InitialContext();
    dataSource = (DataSource)initialContext.lookup(propiedades.getPropiedad("datasource.JNDI"));
}

public static Connection getConexion() throws SQLException, IOException, NamingException{
    if (dataSource== null)
        setupDataSource();
    return dataSource.getConnection();
}}

Besides it is advisable to close the connection at every access to data. Or handle a single connection per action method managed bean to pass this parameter and make a single close.

Thanks for your help

Answers


Logically, you should manage transactions and connections at the top level of your services/stateless business logic objects. The commonly accepted approach for that is to rely on a dependency injection library to inject the connection/transaction handling aspect into the application. Take a look at Spring, specifically Spring JDBC.


Need Your Help

Use WCF to implement services according to industry standard?

c# .net wcf web-services messaging

I am looking for a proper framework to implement the (web)service tier in .NET. I am a little bit familiar with WCF and wondering, if it can be customized the way I want it.

How to use the TextWatcher class in Android?

android android-edittext textwatcher

Can anyone tell me how to mask the substring in EditText or how to change EditText substring input to password type or replace by another character like this 123xxxxxxxxx3455

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.