2011-06-23 132 views
1

你好请帮我一下:用jdbc管理连接数据库jsf

我需要知道jsf中的管理连接。 我使用JSF 1.1并使用JDBC。

我的项目有一个结构MVC。 我的连接是从数据源获取的。

问题1: 我应该在包中创建我的连接工厂类。 问题2: dao,businesslogig,backingbean包中的正确托管提交和回滚在哪里? 问题4: dao,businesslogig,backingbean包中的正确托管打开和关闭连接在哪里?

我的连接工厂类是下面这样

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(); 
}} 

而且最好是收于每次访问数据的连接。 或者处理每个操作方法托管bean的单个连接以传递此参数并关闭一次。

感谢您的帮助

回答

0

从逻辑上讲,你应该在你的服务/无国籍业务逻辑对象的顶层管理事务和连接。通常可以接受的方法是依赖依赖注入库来将连接/事务处理方面注入应用程序。看看Spring,特别是Spring JDBC。