为什么数据库连接经常在两个位置关闭,一次是在使用后直接关闭,其次是在finally块中使用null检查以防止它们被关闭两次.使用finally块是不够的?在每种情况下都要执行finally块.
这是Apache-Tomcat JNDI数据源HOW-TO的官方示例.他们指出,在任何情况下都必须关闭连接.我想知道为什么使用finally-block是不够的,因为主try {} -block结束时的close-commands似乎是redundent.
Connection conn = null;
Statement stmt = null; // Or PreparedStatement if needed
ResultSet rs = null;
try
{
conn = ... get connection from connection pool ...
stmt = conn.createStatement("select ...");
rs = stmt.executeQuery();
... iterate through the result set ...
rs.close ();
rs = null;
stmt.close ();
stmt = null;
conn.close (); // Return to connection pool
conn = null; // Make sure we don't close it twice
}
catch (SQLException …Run Code Online (Sandbox Code Playgroud) 我已经在Tomcat安装上部署了多个swallowOutput="true"Web应用程序,并且正在我的context.xml中使用该应用程序,以实现不同的“每个webapp”日志输出,而不仅仅是所有webapp的一个大“ catalina.out”。那工作很好。因此,我为每个Web应用程序使用logging.properties文件。
Webapp正在使用basic生成日志输出System.out.println。
现在,我想通过指定以下内容来定义由Tomcat生成的非常简单的日志格式:
java.util.logging.SimpleFormatter.format=%5$s %n
Run Code Online (Sandbox Code Playgroud)
但是,无论我指定了哪种格式字符串,生成的日志始终显示我不需要的完整格式:
22-Jun-2019 16:08:15.310 INFO [http-nio-80-exec-10] org.apache.catalina.core.StandardWrapperValve.invoke backend Sat Jun 22 16:08:15 CEST 2019: ... output of the servlet's System.out.println-calls ...
Run Code Online (Sandbox Code Playgroud)
我想生成一个小的且布局清晰的日志,而每行中都没有“ StandardWrapperValve”等标记。我的全局logging.properties文件的格式如下
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler
.handlers …Run Code Online (Sandbox Code Playgroud) 我想初始化Jersey Rest服务并引入一个全局应用程序范围的变量,该变量应该在应用程序启动时计算,并且应该在每个rest资源和每个方法中可用(此处由整数globalAppValue = 17表示,但将是一个复杂的对象后来).
为了初始化服务并在启动时计算一次值,我发现了两种做法:一般的ServletContextListener和Jersey ResourceConfig方法.但是我还没有理解他们俩之间有什么区别?两种方法在启动时触发(两者都打印System.out-messages).
这是我的ServletContextListener的实现,它工作正常:
public class LoadConfigurationListener implements ServletContextListener
{
private int globalAppValue = 17;
@Override
public void contextDestroyed (ServletContextEvent event)
{
}
@Override
public void contextInitialized (ServletContextEvent event)
{
System.out.println ("ServletContext init.");
ServletContext context = event.getServletContext ();
context.setAttribute ("globalAppValue", globalAppValue);
}
}
Run Code Online (Sandbox Code Playgroud)
这是Jersey Rest ResourceConfig方法的实现,其中ServletContext不可用.以后可以通过使用资源方法注入来提供此Application对象:
@ApplicationPath("Resources")
public class MyApplication extends ResourceConfig
{
@Context
ServletContext context;
private int globalAppValue = 17;
public MyApplication () throws NamingException
{
System.out.println ("Application init.");
// returns NullPointerException since ServletContext is not …Run Code Online (Sandbox Code Playgroud)