Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 1998-2016 Caucho Technology -- all rights reserved
*
* This file is part of Baratine(TM)
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Baratine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Baratine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Baratine; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Nam Nguyen
*/
package com.caucho.v5.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.caucho.v5.io.IoUtil;
import io.baratine.service.OnDestroy;
import io.baratine.service.OnInit;
import io.baratine.service.Result;
import io.baratine.service.Service;
@Service
public class JdbcConnectionImpl implements JdbcService
{
private Logger _logger = Logger.getLogger(JdbcConnectionImpl.class.toString());
private String _url;
private Properties _props;
private Connection _conn;
private int _id;
private String _testQueryBefore;
private String _testQueryAfter;
public JdbcConnectionImpl(int id, String url, Properties props,
String testQueryBefore, String testQueryAfter)
{
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "constructor: id=" + id + ", url=" + toDebugSafe(url));
}
_id = id;
_url = url;
_props = props;
_testQueryBefore = testQueryBefore;
_testQueryBefore = testQueryAfter;
}
@OnInit
public void onInit(Result result)
{
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "onInit: id=" + _id + ", url=" + toDebugSafe(_url));
}
try {
connect();
result.ok(null);
}
catch (SQLException e) {
result.fail(e);
}
}
private void reconnect()
{
_logger.log(Level.FINE, "reconnect: id=" + _id);
try {
IoUtil.close(_conn);
connect();
}
catch (SQLException e) {
_logger.log(Level.FINE, "failed to reconnect: id=" + _id + ", url=" + toDebugSafe(_url), e);
}
}
private void connect()
throws SQLException
{
_logger.log(Level.FINE, "connect: id=" + _id + ", url=" + toDebugSafe(_url));
_conn = DriverManager.getConnection(_url, _props);
}
@Override
public void execute(Result result, String sql, Object ... params)
{
if (_logger.isLoggable(Level.FINER)) {
_logger.log(Level.FINER, "execute: id=" + _id + ", sql=" + toDebugSafe(sql));
}
testQueryBefore();
try {
int updateCount = execute(sql, params);
result.ok(updateCount);
testQueryAfter();
}
catch (SQLException e) {
reconnect();
result.fail(e);
}
}
@Override
public void executeBatch(Result> result, String sql, List