Resource injection in DAO?
I use a Data Acces Object pattern in a Java EE 5 Web-Tier Application.
The servlets (and JSP) in the front-end call a separate object that that hides the implementation of accessing data in the databases and maintains the connection to databases.
Can I use the new Resource Injection annotations to simplify the JNDI and DataSoucce code in my DAO class?
I have read that Resource Injection only works in Container managed objects like Servlets (but not in JSP which can be complied after deployment).
Can you point me to a runnable sample application that uses Resource Injection to simplify the JNDI and DataSoucre code?
# 1
> Can I use the new Resource Injection annotations to
> simplify the JNDI and DataSoucce code in my DAO
> class?
>
> I have read that Resource Injection only works in
> Container managed objects like Servlets (but not in
> JSP which can be complied after deployment).
That is correct.
>
> Can you point me to a runnable sample application
> that uses Resource Injection to simplify the JNDI and
> DataSoucre code?
http://java.sun.com/developer/technicalArticles/J2EE/injection/
One thing you could do in your case would be to use a Servlet's init(...) method with the Resource Injection to initialize your DAO with the resource of interest. Or you could continue to use the JNDI method.
# 2
> Can I use the new Resource Injection annotations to
> simplify the JNDI and DataSoucce code in my DAO
> class?
>
> I have read that Resource Injection only works in
> Container managed objects like Servlets (but not in
> JSP which can be complied after deployment).
That is correct.
>
> Can you point me to a runnable sample application
> that uses Resource Injection to simplify the JNDI and
> DataSoucre code?
http://java.sun.com/developer/technicalArticles/J2EE/injection/
One thing you could do in your case would be to use a Servlet's init(...) method with the Resource Injection to initialize your DAO with the resource of interest. Or you could continue to use the JNDI method.
# 5
Sample Application Listener
package com.eshop.web;
import com.eshop.biz.DBAccessor;
import com.eshop.biz.ShoppingCart;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
/**
*
* @author Aliyu Musa
* @version
*
* Web application lifecycle listener.
*/
public class ShopListener implements ServletContextListener, HttpSessionListener {
@PersistenceUnit(name="e-unit")
private EntityManagerFactory emf;
/**
* ### Method from ServletContextListener ###
*
* Called when a Web application is first ready to process requests
* (i.e. on Web server startup and when a context is added or reloaded).
*
* For example, here might be database connections established
* and added to the servlet context attributes.
*/
public void contextInitialized(ServletContextEvent evt) {
DBAccessor db=new DBAccessor(emf);
evt.getServletContext().setAttribute("db",db);
}
/**
* ### Method from ServletContextListener ###
*
* Called when a Web application is about to be shut down
* (i.e. on Web server shutdown or when a context is removed or reloaded).
* Request handling will be stopped before this method is called.
*
* For example, the database connections can be closed here.
*/
public void contextDestroyed(ServletContextEvent evt) {
// TODO add your code here e.g.:
/*
Connection con = (Connection) e.getServletContext().getAttribute("con");
try { con.close(); } catch (SQLException ignored) { } // close connection
*/
}
/**
* ### Method from HttpSessionListener ###
*
* Called when a session is created.
*/
public void sessionCreated(HttpSessionEvent evt) {
ShoppingCart cart=new ShoppingCart();
evt.getSession().setAttribute("cart",cart);
evt.getSession().setMaxInactiveInterval(10*60*60);
}
/**
* ### Method from HttpSessionListener ###
*
* Called when a session is destroyed(invalidated).
*/
public void sessionDestroyed(HttpSessionEvent evt) {
evt.getSession().invalidate();
}
}