我有一个在节点之间建立关系的 servlet,它在循环中这样做,它可能会在循环中创建 1-999 或更多关系。它通过将数组列表中的数据加载到 Neo4j 中来实现。当我第一次运行它时,它运行良好。第二次,如果我再次跑 5 秒,它会给出一个 java.lang.NullPointerException
. 大约一分钟后,它再次运行良好。这是servlet代码
public class InputDebtDataToNeo4j extends HttpServlet {
static GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("C:\\Users\\Jovo\\Documents\\NetBeansProjects\\DebtSolutions\\build\\web\\NEO4J databases\\db1");
//Neo4jSinletonDbStart nn=new Neo4jSinletonDbStart();
//GraphDatabaseService graphDB =nn.getInstance();
private ArrayList<InputData> l111;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession(true);
l111= (ArrayList<InputData>) session.getAttribute("hasdata");
long mynodenumber;
mynodenumber = Long.parseLong(session.getAttribute("node_id").toString());
Transaction tx = graphDB.beginTx();
try {
Node firstNode;
Node secondNode;
for (InputData element : l111)
{
Relationship relationship = null;
firstNode=graphDB.getNodeById(mynodenumber);
secondNode=graphDB.getNodeById(element.getNodeidnumber());
relationship = firstNode.createRelationshipTo( secondNode, RelTypes.OWES );
relationship.setProperty( "amount", "'"+element.getDebtamount()+"'" );
out.println( relationship.getStartNode().toString());
out.println( relationship.getProperty( "amount" ) );
out.println( relationship.getEndNode().toString() );
}
tx.success();
//response.sendRedirect("DebtSolutions.jsp");
}
catch(Exception e )
{
tx.failure();
out.println(e.toString());
}
finally {
tx.finish();
graphDB.shutdown();
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我得到的是
org.neo4j.graphdb.TransactionFailureException: Failed to mark transaction as rollback only
我注意到的是,当我在 Netbeans 中进行更改时,它总是可以正常工作,例如按 Enter 输入一个空的换行符并保存,servlet 重新加载 - 然后以其他方式启动 Neo4j,它给出了异常。