我有一个JSF网页和表单。在提交时,我验证输入的型号。当我在JUnit中运行一些抛出异常的代码时,我的所有测试都通过了。当我输入型号时,我是唯一登录此表格的人,它的工作正常。我认为这是各种各样的竞争条件......
失败的代码就是这个
public static int getCountOfSameModelsInItemInventory(String[] models) {
int count = 0;
if (initialize()) {
try (Connection conn = DriverManager.getConnection(AOurl, userName, password)) {
count = TryWithConnection(models, conn);
} catch (SQLException ex) {
Logger.getLogger(SqlInformationHolder.class.getName()).log(Level.SEVERE, null, ex);
}
}
return count;
}
行号表示此处失败try (Connection conn = DriverManager.getConnection(AOurl, userName, password)) {
我有一个C#背景,所以我的第一直觉是在我的Connection周围使用一个锁,但我不认为java有这个。我的研究表明,如果端口已经被使用,则会抛出错误。我不确定这是否是足够的信息,但如果需要,我会编辑我的帖子。
所以我想最后我有2个问题......显而易见的问题:"我如何解决这个问题",还有两个设计模式/最佳实践有助于避免这种情况吗?
平均而言,我有大约5个人使用网页,这是调用此特定服务器的唯一方法。其他方法使用不同的SQL服务器。
修改
所以我有另外一个方法来调用这个服务器。我正在查看它是否已清除所有资源。
修改
Oct 08, 2014 9:17:09 AM dli_qc_v2.SqlInformationHolder getCountOfSameModelsInItemInventory
SEVERE: null
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host OURSERVER, named instance THEINSTNACE failed. Error: "java.net.BindException: Address already in use: Cannot bind". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dli_qc_v2.SqlInformationHolder.getCountOfSameModelsInItemInventory(SqlInformationHolder.java:110)
at dli_qc_v2.DLIModelValidator.validate(DLIModelValidator.java:56)
at dli_qc_v2.NcpFormBean.verifyingModelValue(NcpFormBean.java:327)
at dli_qc_v2.NcpFormBean.fillForm(NcpFormBean.java:230)
at sun.reflect.GeneratedMethodAccessor447.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
at javax.faces.component.UICommand.broadcast(UICommand.java:120)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:937)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:271)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1249)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:675)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2442)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2431)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)