Example usage for java.sql Connection isClosed

List of usage examples for java.sql Connection isClosed

Introduction

In this page you can find the example usage for java.sql Connection isClosed.

Prototype

boolean isClosed() throws SQLException;

Source Link

Document

Retrieves whether this Connection object has been closed.

Usage

From source file:org.squale.welcom.outils.jdbc.WConnectionPoolMagic.java

/**
 * @return Retourne le type de connection pour le projet
 *//*from   ww w  .j a  v  a 2s . c om*/
private int searchConnection() {
    Connection connection = null;

    try {
        // Test Hybernate
        connection = getHibernateConnection();
        if (connection != null) {
            if (!connection.isClosed()) {
                connection.close();
            }
            WSessionHibernatePersitance.close(connection);
            return MODE_CONNECTION_HYBERNATE;
        }

        // Test JDBC
        if (ConnectionPool.isInitalized()) {
            connection = ConnectionPool.getConnection();
            if (connection != null) {
                return MODE_CONNECTION_JDBC;
            }
        }
        // test MJDBC
        connection = WConnectionPool.getConnection();
        if (connection != null) {
            return MODE_CONNECTION_MJDBC;
        } else {
            return MODE_CONNECTION_UNKNOW;
        }
    } catch (final SQLException e) {
        log.error(e, e);
        return MODE_CONNECTION_UNKNOW;
    }
}

From source file:org.openmrs.module.jasperreport.ReportGenerator.java

public synchronized static void generateHtmlAndWriteToResponse(JasperReport report, HashMap<String, Object> map,
        HttpServletResponse response) throws IOException {

    FileInputStream fileInputStream = getReportInputStream(report);

    String url = Context.getRuntimeProperties().getProperty("connection.url", null);

    Connection conn;
    try {//from   ww w . ja  v  a 2s.  c om
        conn = connect(url);
    } catch (SQLException e) {
        log.error("Error connecting to DB.", e);
        return;
    }

    String reportDirPath = JasperUtil.getReportDirPath();
    map.put("connection", conn);
    map.put("SUBREPORT_DIR", reportDirPath + File.separator + report.getReportId() + File.separator);

    log.debug("Report parameter map: " + map);

    JasperPrint jasperPrint = null;
    try {
        // generate the report and write it to file
        jasperPrint = JasperFillManager.fillReport(fileInputStream, map, conn);
        JRHtmlExporter exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());
        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);
        exporter.exportReport();
    } catch (JRException e) {
        log.error("Error generating report", e);
    } finally {
        try {
            if (!conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            log.error("Exception closing report connection.", e);
        }
    }
}

From source file:pivotal.au.se.gemfirexdweb.controller.PrefsController.java

@RequestMapping(value = "/preferences", method = RequestMethod.GET)
public String showPreferences(Model model, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws Exception {
    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {/*from www .j ava2 s.  co m*/
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to show preferences");

    model.addAttribute("userPref", (UserPref) session.getAttribute("prefs"));

    // This will resolve to /WEB-INF/jsp/preferences.jsp
    return "preferences";
}

From source file:org.mybatis.spring.SqlSessionTemplateTest.java

@Test
public void testGetConnection() throws java.sql.SQLException {
    java.sql.Connection con = sqlSessionTemplate.getConnection();

    // outside of an explicit tx, getConnection() will start a tx, get an open connection then
    // end the tx, which closes the connection
    assertTrue(con.isClosed());
}

From source file:pivotal.au.se.gemfirexdweb.controller.MemberController.java

@RequestMapping(value = "/members", method = RequestMethod.GET)
public String showDiskstores(Model model, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws Exception {
    javax.servlet.jsp.jstl.sql.Result allMemberInfoResult = null;

    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {//from   w w w .j a v  a  2 s .c  o m
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to show members");

    MemberDAO mbrDAO = GemFireXDWebDAOFactory.getMemberDAO();

    String memberAction = request.getParameter("memberAction");
    String propertyAction = request.getParameter("propertyAction");

    if (memberAction != null) {
        logger.debug("memberAction = " + memberAction);
        logger.debug("propertyAction = " + propertyAction);

        if (memberAction.equals("ALLMEMBEREVENTINFO")) {
            allMemberInfoResult = mbrDAO.getMemberInfo((String) request.getParameter("memberId"),
                    (String) session.getAttribute("user_key"));
            model.addAttribute("allMemberInfoResult", allMemberInfoResult);
            model.addAttribute("memberid", (String) request.getParameter("memberId"));
        } else if (memberAction.equals("PROPERTIES")) {
            String props = mbrDAO.getMemberProperties((String) request.getParameter("memberId"), propertyAction,
                    (String) session.getAttribute("user_key"));

            model.addAttribute("props", props);
            model.addAttribute("propertyAction", propertyAction);
            model.addAttribute("memberid", (String) request.getParameter("memberId"));
        }
    }

    List<Member> members = mbrDAO.retrieveMembers((String) session.getAttribute("user_key"));

    model.addAttribute("members", members);
    model.addAttribute("records", members.size());
    model.addAttribute("estimatedrecords", members.size());

    // This will resolve to /WEB-INF/jsp/members.jsp
    return "members";
}

From source file:pivotal.au.se.gemfirexdweb.controller.PrefsController.java

@RequestMapping(value = "/preferences", method = RequestMethod.POST)
public String updatePreferences(Model model, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws Exception {
    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {//w w w  .jav a  2s.c  o  m
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    logger.debug("Received request to update preferences");

    UserPref userPrefs = (UserPref) session.getAttribute("prefs");

    userPrefs.setRecordsToDisplay(Integer.parseInt(request.getParameter("recordsToDisplay")));
    userPrefs.setMaxRecordsinSQLQueryWindow(
            Integer.parseInt(request.getParameter("maxRecordsinSQLQueryWindow")));
    userPrefs.setAutoCommit((String) request.getParameter("autoCommit"));
    userPrefs.setJolokiaURL((String) request.getParameter("jolokiaURL"));

    ConnectionManager cm = ConnectionManager.getInstance();
    Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));

    if (request.getParameter("autoCommit").equals("Y")) {
        conn.setAutoCommit(true);
    } else {
        conn.setAutoCommit(false);
    }

    cm.updateConnection(conn, (String) session.getAttribute("user_key"));

    model.addAttribute("saved", "Successfully saved application preferences");
    session.setAttribute("userPref", userPrefs);

    // This will resolve to /WEB-INF/jsp/preferences.jsp
    return "preferences";
}

From source file:org.openmrs.module.jasperreport.ReportGenerator.java

/**
 * @param report//  www  .  j  av  a 2s.  com
 * @param map
 * @return
 */
public synchronized static File generate(JasperReport report, HashMap<String, Object> map, boolean appendDate,
        boolean pdfAutoPrint, String format) throws IOException {

    String reportDirPath = JasperUtil.getReportDirPath();

    String exportPath = reportDirPath + File.separator + JasperReportConstants.GENERATED_REPORT_DIR_NAME
            + File.separator + report.getName().replaceAll("\\W", "")
            + (appendDate ? new SimpleDateFormat("dd-MM-yyyy-HH-mm", JasperUtil.getLocale()).format(new Date())
                    : "");

    FileInputStream fileInputStream = getReportInputStream(report);

    String url = Context.getRuntimeProperties().getProperty("connection.url", null);
    Connection conn;
    try {
        conn = connect(url);
    } catch (SQLException e) {
        log.error("Error connecting to DB.", e);
        return null;
    }

    map.put("connection", conn);
    map.put("SUBREPORT_DIR", reportDirPath + File.separator + report.getReportId() + File.separator);

    log.debug("Report parameter map: " + map);

    JasperPrint jasperPrint = null;
    try {
        // generate the report and write it to file
        jasperPrint = JasperFillManager.fillReport(fileInputStream, map, conn);

        if (StringUtils.equalsIgnoreCase(format, "pdf")) {
            exportPath += ".pdf";
            exportPDFFormat(jasperPrint, exportPath, pdfAutoPrint);
        } else if (StringUtils.equalsIgnoreCase(format, "excel")) {
            exportPath += ".xlsx";
            exportExcelFormat(jasperPrint, exportPath);
        } else {
            log.error("Unknown format " + format);
        }

    } catch (JRException e) {
        log.error("Error generating report", e);
    } finally {
        try {
            if (!conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            log.error("Exception closing report connection.", e);
        }
    }

    return new File(exportPath);
}

From source file:com.viettel.logistic.wms.service.SerialInventoryServiceImpl.java

private void rollback(Session session, Transaction transaction, Connection con) {
    try {// w ww  .  j  a va2 s .c o m
        if (con != null) {
            con.rollback();
        }
        if (transaction != null) {
            transaction.rollback();
        }

        if (con != null && !con.isClosed()) {
            con.close();
        }
    } catch (SQLException ex) {
        Logger.getLogger(StockImportServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (session != null && session.isOpen()) {
        session.close();
    }
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testConnectionPoolReget() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);/*from  w w w .j a  v  a  2s .  c  om*/

    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();

    // now reget a connection
    Connection con2 = pc.getConnection();

    // assert that the first connection is closed.
    assertTrue(con.isClosed(), "First connection is not closed");
}

From source file:org.josso.gateway.identity.service.store.db.AbstractDBIdentityStore.java

/**
 * Close the given db connection.//from  ww w .j a v  a2s. co m
 *
 * @param dbConnection
 * @throws SSOIdentityException
 */
protected void closeDBConnection(Connection dbConnection) throws SSOIdentityException {

    try {
        if (dbConnection != null && !dbConnection.isClosed()) {
            dbConnection.close();
        }
    } catch (SQLException se) {
        if (logger.isDebugEnabled()) {
            logger.debug("Error while clossing connection");
        }

        throw new SSOIdentityException("Error while clossing connection\n" + se.getMessage());
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Error while clossing connection");
        }

        throw new SSOIdentityException("Error while clossing connection\n" + e.getMessage());

    }

}