Acontece o erro na segunda requisição ao injetar o EntityManager
java.lang.IllegalStateException: EntityManager is closed
at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:105)
at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:96)
at org.hibernate.jpa.internal.EntityManagerImpl.getSession(EntityManagerImpl.java:111)
at org.hibernate.jpa.internal.TransactionImpl.getSession(TransactionImpl.java:51)
at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:61)
at br.com.cloudapp.pagamento.dao.JPAPagamentoIpnDAO.adiciona(JPAPagamentoIpnDAO.java:20)
at br.com.cloudapp.pagamento.controllerIPN.IPNListenerServlet.doPost(IPNListenerServlet.java:146)
public class IPNListenerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IpnConfig ipnConfig;
@Inject
private EntityManager manager;
public IPNListenerServlet(){
}
public IPNListenerServlet(EntityManager manager){
this.manager = manager;
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PagamentoIPN ipnInfo = new PagamentoIPN();
LoggingManager.info(IPNListenerServlet.class, " Inicio IPN ");
try{
Enumeration reqParamNames = request.getParameterNames();
StringBuilder cmd1 = new StringBuilder();
String pName;
String pValue;
cmd1.append("cmd=_notify-validate");
while (reqParamNames.hasMoreElements()) {
pName = (String) reqParamNames.nextElement();
pValue = request.getParameter(pName);
try{
cmd1.append("&").append(pName).append("=").append(pValue);
}
catch(Exception e){
e.printStackTrace();
}
}
URL u = new URL(Config.URL_PAYPAL);
HttpsURLConnection con = (HttpsURLConnection) u.openConnection();
con.setRequestProperty("Host", Config.URL_PAYPAL);
con.setRequestProperty("Content-length", String.valueOf(cmd1.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(cmd1.toString());
output.flush();
output.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String res = in.readLine();
in.close();
ipnInfo = populaNotificacao2(request, ipnInfo, res);
if (res.equals("VERIFIED")) {
if(ipnInfo.getPaymentStatus() == null || !ipnInfo.getPaymentStatus().equalsIgnoreCase("COMPLETED"))
ipnInfo.setError("IF- payment_status IS NOT COMPLETED {" + ipnInfo.getPaymentStatus() + "}");
} else
ipnInfo.setError("Invalida a resposta {" + res + "} esperava: {VERIFIED}");
}
catch(Exception e)
{
LoggingManager.info(IPNListenerServlet.class, "Exception -->> "+ e.toString());
}
new JPAPagamentoIpnDAO(manager).adiciona(ipnInfo);
manager.close();
LoggingManager.info(IPNListenerServlet.class, " Final IPN ");
}
private PagamentoIPN populaNotificacao2(HttpServletRequest request, PagamentoIPN ipnInfo,String res) {
ipnInfo.setLogTime(System.currentTimeMillis());
ipnInfo.setItemName("item_name");
ipnInfo.setItemNumber("itemNumber");
ipnInfo.setQuantity("quantity");
ipnInfo.setPaymentStatus(request.getParameter("payment_status"));
ipnInfo.setMcGross(request.getParameter("mc_gross"));
ipnInfo.setMcCurrency(request.getParameter("mc_currency"));
ipnInfo.setTxnId(request.getParameter("txn_id"));
ipnInfo.setReceiverEmail(request.getParameter("receiver_email"));
ipnInfo.setPayerEmail(request.getParameter("payer_email"));
ipnInfo.setResponse(res);
return ipnInfo;
}