这里我遇到连接问题。
我在Tomcat 7中部署了一个Web服务。在Web服务中,我使用Filter,在init()方法中,我调用了连接。
public static Connection connection = null;
@Override
public void init(FilterConfig config) throws ServletException {
connection = ConnectionFactoryJndi.getConnection();
}
现在我在整个应用程序中使用相同的连接对象。因此,这样我只创建一个连接并执行sql查询。它运行正常。但是当我访问服务网址时;它给出错误“无法连接;连接关闭后没有操作”我也设置了maxWait =“28800000”即8小时;最长持续时间 我的问题是如何让它始终保持开放。或者当我访问url时,它不应该给出相同的错误并且能够自动连接。我正在使用MySql。
这是server.xml conf -
<Resource
name="jdbc/xxxxx"
auth="Container"
type="javax.sql.DataSource"
maxActive="200"
maxIdle="30"
maxWait="28800000"
username="xxx"
password="xxxxx"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/xxxxx" />
在ConnectionFactoryJndi类中 -
public Connection createConnection(){
Connection connection = null;
try {
try {
envContext = new InitialContext();
} catch (NamingException e) {
}
Context initContext = null;
try {
initContext = (Context) envContext.lookup("java:/comp/env");
} catch (NamingException e) {
}
DataSource datasource = null;
try {
datasource = (DataSource) initContext.lookup("jdbc/xxxxxx");
} catch (NamingException e) {
System.out.println("Name not found");
}
connection = datasource.getConnection();
} catch (SQLException e) {
System.out.println("ERROR: Unable to Connect to Database.");
}
return connection;
}
请建议最佳做法。
感谢。
服务器日志 -
Dec 05, 2013 10:09:44 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [ServletAdaptor] in context with path [/IntellixWebApi] threw exception
java.lang.NullPointerException
at com.astroved.intellix.jdbc.dao.ClientDAO.getClientDetails(ClientDAO.java:44)
at com.astroved.intellix.security.ValidateClient.isValidClient(ValidateClient.java:14)
at com.astroved.intellix.security.SecurityFilter.doFilter(SecurityFilter.java:110)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
然而,它是由于连接失败而来的;然后无法执行查询。
ClientDao类 -
ResultSet rs = null;
try {
try {
statement = connection.createStatement();
} catch (SQLException e) {
log.error("Error in Client DAO class (unable to connect connection) "
+ e.getMessage());
// e.printStackTrace();
}
try {
rs = statement.executeQuery(query);
} catch (SQLException e) {
log.error("Error in Client DAO class (unable to Execute Query ) "
+ e.getMessage());
// e.printStackTrace();
}
try {
while (rs.next()) {
client = new ClientTO();
client.setClientId(rs.getInt("clientid"));
client.setClientUrl(rs.getString("clienturl"));
client.setClientSecurityKey(rs
.getString("clientserviceapiKey"));
clientList.add(client);
}
} catch (SQLException e) {
log.error("Error in Client DAO class ( Error in iteration ) "
+ e.getMessage());
// e.printStackTrace();
}
} finally {
DbUtil.close(rs);
DbUtil.close(statement);
//DbUtil.close(connection);
}
私人连接连接= SecurityFilter.connection;
Server.xml -
<Resource
name="jdbc/xxxxx"
auth="Container"
type="javax.sql.DataSource"
maxActive="200"
maxIdle="30"
maxWait="28800000"
username="xxxxxx"
password="xxxxxx"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/xxxxxx" />
</GlobalNamingResources>
客户端DAO类 -
ResultSet rs = null;
try {
try {
connection = ConnectionFactoryJndi.getConnection();
statement = connection.createStatement();
rs = statement.executeQuery(query);
} catch (SQLException e) {
log.error("Error in Client DnaReportDao class (unable to Execute Query ) "+ e.getMessage());
}
try {
if(rs != null) {
while (rs.next()) {
codeValue = new DnaReportTO();
codeValue.setNature(rs.getString("Nature"));
codeValue.setInterestedSubject(rs.getString("InterestdSubject"));
codeValue.setTipToParent(rs.getString("TipsToParent"));
}
}
} catch (SQLException e) {
log.error("Error in DnaReportDao class ( Error in iteration ) "
+ e.getMessage());
}
} finally {
DbUtil.close(rs);
DbUtil.close(statement);
}
Currenr服务器日志
Dec 05, 2013 12:22:22 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.astroved.intellix.resource.DnaReportResource, and Java type class com.astroved.intellix.resource.DnaReportResource, and MIME media type application/xml was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.astroved.intellix.security.SecurityFilter.doFilter(SecurityFilter.java:112)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.astroved.intellix.resource.DnaReportResource, and Java type class com.astroved.intellix.resource.DnaReportResource, and MIME media type application/xml was not found
... 27 more
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
ERROR: Unable to Connect to Database.
答案 0 :(得分:1)
我在整个应用程序中使用相同的连接对象
这不起作用。您应该使用连接池。否则你必须自己做两件事,连接池会自动避免:
不要这样做。使用Tomcat连接池。这很容易。
答案 1 :(得分:0)
从您在此处给出的错误看来,应用程序似乎已关闭连接。错误no operation after connection closed
可能真的意味着您的应用程序中的某些内容正在关闭它。如果没有日志,就很难诊断导致问题的原因。
使用连接池的正确方法是在每次需要时借用连接。永远不要将对您自己的类中的连接的引用存储为单例或静态字段。这是因为池可能会在一段时间后重新启动连接,并且最终会得到一个有效的连接对象,该对象将被关闭。这可能也是你遇到问题的原因。正确的方法如下:
ResultSet rs = null;
try {
try {
connection = ConnectionFactoryJndi.getConnection(); //always do this everytime you need a conenction.
statement = connection.createStatement();
} catch (SQLException e) {
log.error("Error in Client DAO class (unable to connect connection) "
+ e.getMessage());
// e.printStackTrace();
}
try {
if(statement != null){
rs = statement.executeQuery(query);
}else{
//log error and return or throw exception
}
} catch (SQLException e) {
log.error("Error in Client DAO class (unable to Execute Query ) "
+ e.getMessage());
// e.printStackTrace();
}
try {
if(rs != null){
while (rs.next()) {
client = new ClientTO();
client.setClientId(rs.getInt("clientid"));
client.setClientUrl(rs.getString("clienturl"));
client.setClientSecurityKey(rs
.getString("clientserviceapiKey"));
clientList.add(client);
}
}else{
//log error and return or throw exception
}
} catch (SQLException e) {
log.error("Error in Client DAO class ( Error in iteration ) "
+ e.getMessage());
// e.printStackTrace();
}
} finally {
//close rs
//close statement
//never close a conenction
}