想寫這篇文章
在EJB容器
一
而在WEB容器中
要在WEB容器中讓容器管理持久化事務
事實上確實一些WEB容器不能注入EntityManager
<?xml version=
<persistence xmlns=
xmlns:xsi=
xsi:schemaLocation=
version=
<persistence
<jta
<class>com
</persistence
</persistence>
<?xml version=
<persistence xmlns=
xmlns:xsi=
xsi:schemaLocation=
version=
<persistence
<jta
<class>com
</persistence
</persistence>只要這個persistence
//為了閱讀方便刪除了無關內容和注釋
<PRE class=java name=
@PersistenceContext(name=
private EntityManager em;
@Resource
private UserTransaction utx;
protected void service(HttpServletRequest request
throws ServletException
PrintWriter out = response
CustomerEO ce = new CustomerEO();
ce
ce
ce
ce
utx
em
mit();
//容器管理事務不需要自己手工回滾
}
}
</PRE>
public class TestAxman extends HttpServlet {
@PersistenceContext(name=
private EntityManager em;
@Resource
private UserTransaction utx;
protected void service(HttpServletRequest request
throws ServletException
PrintWriter out = response
CustomerEO ce = new CustomerEO();
ce
ce
ce
ce
utx
em
mit();
//容器管理事務不需要自己手工回滾
}
}
public class TestAxman extends HttpServlet
{
@PersistenceContext(name=
private EntityManager em;
@Resource
private UserTransaction utx;
protected void service(HttpServletRequest request
throws ServletException
PrintWriter out = response
CustomerEO ce = new CustomerEO();
ce
ce
ce
ce
utx
em
mit();
//容器管理事務不需要自己手工回滾
}
}
這樣注入進來的EntityManager完全由容器管理
但是任何事物都有兩面性
通過JNDI查找的jndi
@PersistenceContext(name=
public class TestAxman extends HttpServlet {
protected void service(HttpServletRequest request
throws ServletException
Context env =(Context)newInitialContext()
EntityManager em = (EntityManager)env
Query query = em
List <CustomerEO> ls = query
}
}
@PersistenceContext(name=
public class TestAxman extends HttpServlet {
protected void service(HttpServletRequest request
throws ServletException
Context env =(Context)newInitialContext()
EntityManager em = (EntityManager)env
Query query = em
List <CustomerEO> ls = query
}
}如果你不想用注釋可以在web
<persistence
<persistence
<persistence
</persistence
<persistence
<persistence
<persistence
</persistence
Context env =(Context)new InitialContext()
EntityManager em = (EntityManager)env
需要說明的是
public class TestAxman extends HttpServlet {
Context env = null;
protected void init(ServletConfig config) throws ServletException {
env = (Context)newInitialContext()
}
protected void service(HttpServletRequest request
throws ServletException
EntityManager em = (EntityManager)env
Query query = em
List <CustomerEO> ls = query
}
}
public class TestAxman extends HttpServlet {
Context env = null;
protected void init(ServletConfig config) throws ServletException {
env = (Context)newInitialContext()
}
protected void service(HttpServletRequest request
throws ServletException
EntityManager em = (EntityManager)env
Query query = em
List <CustomerEO> ls = query
}
}
<PRE class=java name=
<PRE class=java name=
如果你需要容器管理持久化
方法內的local變量
二
應用管理持久化上下文
要在WEB容器獲取EntityManagerFactory
public class TestServlet extends HttpServlet {
@PersistenceUnit(unitName =
private EntityManagerFactory emf;
public void service(……) throws xxxExceotion{
EntityManager em = emf
try{
//invoke em;
}
finally{ em
//所以必須手工關閉
}
}
public class TestServlet extends HttpServlet {
@PersistenceUnit(unitName =
private EntityManagerFactory emf;
public void service(……) throws xxxExceotion{
EntityManager em = emf
try{
//invoke em;
}
finally{ em
//所以必須手工關閉
}
}如果你不想通過容器注入EntityManagerFactory
EntityManagerFactory emf = Persistence
就可以獲取一個手工創建的EntityManagerFactory
public class TestServlet extends HttpServlet {
private EntityManagerFactory emf;
void init(
emf = Persistence
}
void destory(
if(emf != null) emf
}
public void service(
EntityManager em = emf
try{
//invoke em;
}
finally{ em
//所以必須手工關閉
}
}
public class TestServlet extends HttpServlet {
private EntityManagerFactory emf;
void init(
emf = Persistence
}
void destory(
if(emf != null) emf
}
public void service(
EntityManager em = emf
try{
//invoke em;
}
finally{ em
//所以必須手工關閉
}
}因為EntityManagerFactory是工場對象
同樣上面的那本書中
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26497.html