List of usage examples for java.sql DriverManager setLoginTimeout
public static void setLoginTimeout(int seconds)
From source file:Main.java
private static Connection getHSQLConnection() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); String url = "jdbc:hsqldb:mem:data/tutorial"; DriverManager.setLoginTimeout(60); // fail after 60 seconds return DriverManager.getConnection(url, "sa", ""); }
From source file:com.nextep.designer.sqlgen.sqlite.impl.SQLLiteDatabaseConnector.java
@Override public Connection getConnection(IConnection conn) throws SQLException { final String connURL = getConnectionURL(conn); LOGGER.info(MessageFormat.format(SQLiteMessages.getString("connector.sqlite.connecting"), //$NON-NLS-1$ connURL));/* w w w . j a v a 2 s .c o m*/ Connection connection = null; try { DriverManager.setLoginTimeout(15); connection = DriverManager.getConnection(connURL, conn.getLogin(), conn.getPassword()); } catch (SQLException sqle) { LOGGER.error("Unable to connect to SQLite database: " + sqle.getMessage(), sqle); throw sqle; } LOGGER.info("SQLite connection established"); return connection; }
From source file:com.nextep.designer.sqlgen.mysql.impl.MySQLDatabaseConnector.java
@Override public Connection getConnection(IConnection conn) throws SQLException { final String connURL = getConnectionURL(conn); LOGGER.info(MessageFormat.format(MySQLMessages.getString("capturer.mysql.connecting"), //$NON-NLS-1$ connURL));// w w w .j a va 2 s. co m Connection connection = null; try { DriverManager.setLoginTimeout(15); connection = DriverManager.getConnection(connURL, conn.getLogin(), conn.getPassword()); } catch (SQLException sqle) { LOGGER.error("Unable to connect to MySQL database: " + sqle.getMessage(), sqle); throw sqle; } LOGGER.info("MySQL connection established"); return connection; }
From source file:OracleDataSource.java
public void setLoginTimeout(int timeout) throws SQLException { DriverManager.setLoginTimeout(timeout); }
From source file:br.com.fatecpg.repositories.mysql.MySqlDbProvider.java
private Connection createConnection() { Connection connection = null; try {//from ww w. j a v a 2s . com Class.forName("com.mysql.jdbc.Driver"); DriverManager.setLoginTimeout(1); connection = DriverManager.getConnection(this.mysqlUrl, this.mysqlUsername, this.mysqlPassword); } catch (ClassNotFoundException | SQLException ex) { throw new RuntimeException(ex); } return connection; }
From source file:com.nextep.designer.sqlgen.model.base.AbstractDatabaseConnector.java
/** * This constructor manually load any JDBC drivers prior to version 4.0. As for JDBC 4.0 * drivers, if some are found in the class path, they are automatically loaded by the * <code>DriverManager</code> class when it first attempts to establish a connection. *//*from w w w .j av a 2s .com*/ public AbstractDatabaseConnector() { DriverManager.setLoginTimeout(LOGIN_TIMEOUT); String driverClassName = getJDBCDriverClassName(); if (driverClassName != null) { try { driver = (Driver) Class.forName(driverClassName).newInstance(); } catch (Exception e) { throw new ErrorException(e); } } else { LOGGER.info("This IDatabaseConnector implementation does not provide a JDBC driver, " + "you may use a vendor specific IDatabaseConnector implementation in order to " + "connect to the database"); } }
From source file:com.hangum.tadpole.engine.manager.TadpoleSQLManager.java
/** * <pre>/*w w w .ja v a2s . co m*/ * DB ?. * * ? ??? . * ?? " ' ? ? . * * </pre> * * @param userDB * @return * @throws Exception */ public static SqlMapClient getInstance(final UserDBDAO userDB) throws TadpoleSQLManagerException { SqlMapClient sqlMapClient = null; Connection conn = null; // synchronized (dbManager) { String searchKey = getKey(userDB); try { sqlMapClient = dbManager.get(searchKey); if (sqlMapClient == null) { // oracle ? try { if (userDB.getDBDefine() == DBDefine.ORACLE_DEFAULT | userDB.getDBDefine() == DBDefine.TIBERO_DEFAULT) { DriverManager.setLoginTimeout(10); if (userDB.getLocale() != null && !"".equals(userDB.getLocale())) { Locale.setDefault(new Locale(userDB.getLocale())); } } } catch (Exception e) { logger.error("set locale error", e); } // connection pool ? . sqlMapClient = SQLMap.getInstance(userDB); dbManager.put(searchKey, sqlMapClient); // metadata . conn = sqlMapClient.getDataSource().getConnection(); // don't belive keyword. --;; setMetaData(searchKey, userDB, conn.getMetaData()); } } catch (Exception e) { // String strAddReqInfo = ""; // try { // strAddReqInfo = RequestInfoUtils.requestInfo("db connection exception ", SessionManager.getEMAIL()); // } catch(Exception ee) { // logger.error("request error", ee); // } logger.error("===\n get DB Instance \n seq is " + userDB.getSeq() + "\n", e); dbManager.remove(searchKey); throw new TadpoleSQLManagerException(e); } finally { if (conn != null) try { conn.close(); } catch (Exception e) { } } // } return sqlMapClient; }
From source file:com.mirth.connect.connectors.jdbc.JdbcConnectorService.java
public Object invoke(String method, Object object, String sessionsId) throws Exception { if (method.equals("getInformationSchema")) { // method 'getInformationSchema' will return Set<Table> Connection connection = null; try {//from ww w . ja v a2 s . c o m Properties properties = (Properties) object; String driver = properties.getProperty(DatabaseReaderProperties.DATABASE_DRIVER); String address = properties.getProperty(DatabaseReaderProperties.DATABASE_URL); String user = properties.getProperty(DatabaseReaderProperties.DATABASE_USERNAME); String password = properties.getProperty(DatabaseReaderProperties.DATABASE_PASSWORD); // Although these properties are not persisted, they used by the JdbcConnectorService String tableNamePatternExp = properties .getProperty(DatabaseReaderProperties.DATABASE_TABLE_NAME_PATTERN_EXPRESSION); String selectLimit = properties.getProperty(DatabaseReaderProperties.DATABASE_SELECT_LIMIT); String schema = null; Class.forName(driver); int oldLoginTimeout = DriverManager.getLoginTimeout(); DriverManager.setLoginTimeout(30); connection = DriverManager.getConnection(address, user, password); DriverManager.setLoginTimeout(oldLoginTimeout); DatabaseMetaData dbMetaData = connection.getMetaData(); // the sorted set to hold the table information SortedSet<Table> tableInfoList = new TreeSet<Table>(); // Use a schema if the user name matches one of the schemas. // Fix for Oracle: MIRTH-1045 ResultSet schemasResult = null; try { schemasResult = dbMetaData.getSchemas(); while (schemasResult.next()) { String schemaResult = schemasResult.getString(1); if (user.equalsIgnoreCase(schemaResult)) { schema = schemaResult; } } } finally { if (schemasResult != null) { schemasResult.close(); } } // based on the table name pattern, attempt to retrieve the table information List<String> tablePatternList = translateTableNamePatternExpression(tableNamePatternExp); List<String> tableNameList = new ArrayList<String>(); // go through each possible table name patterns and query for the tables for (String tableNamePattern : tablePatternList) { ResultSet rs = null; try { rs = dbMetaData.getTables(null, schema, tableNamePattern, TABLE_TYPES); // based on the result set, loop through to store the table name so it can be used to // retrieve the table's column information while (rs.next()) { tableNameList.add(rs.getString("TABLE_NAME")); } } finally { if (rs != null) { rs.close(); } } } // for each table, grab their column information for (String tableName : tableNameList) { ResultSet rs = null; ResultSet backupRs = null; boolean fallback = false; try { // apparently it's much more efficient to use ResultSetMetaData to retrieve // column information. So each driver is defined with their own unique SELECT // statement to query the table columns and use ResultSetMetaData to retrieve // the column information. If driver is not defined with the select statement // then we'll define to the generic method of getting column information, but // this could be extremely slow List<Column> columnList = new ArrayList<Column>(); if (StringUtils.isEmpty(selectLimit)) { logger.debug("No select limit is defined, using generic method"); rs = dbMetaData.getColumns(null, null, tableName, null); // retrieve all relevant column information for (int i = 0; rs.next(); i++) { Column column = new Column(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"), rs.getInt("COLUMN_SIZE")); columnList.add(column); } } else { logger.debug( "Select limit is defined, using specific select query : '" + selectLimit + "'"); // replace the '?' with the appropriate schema.table name, and use ResultSetMetaData to // retrieve column information final String schemaTableName = StringUtils.isNotEmpty(schema) ? schema + "." + tableName : tableName; final String queryString = selectLimit.trim().replaceAll("\\?", schemaTableName); Statement statement = connection.createStatement(); try { rs = statement.executeQuery(queryString); ResultSetMetaData rsmd = rs.getMetaData(); // retrieve all relevant column information for (int i = 1; i < rsmd.getColumnCount() + 1; i++) { Column column = new Column(rsmd.getColumnName(i), rsmd.getColumnTypeName(i), rsmd.getPrecision(i)); columnList.add(column); } } catch (SQLException sqle) { logger.info("Failed to execute '" + queryString + "', fall back to generic approach to retrieve column information"); fallback = true; } finally { if (statement != null) { statement.close(); } } // failed to use selectLimit method, so we need to fall back to generic // if this generic approach fails, then there's nothing we can do if (fallback) { // Re-initialize in case some columns were added before failing columnList = new ArrayList<Column>(); logger.debug("Using fallback method for retrieving columns"); backupRs = dbMetaData.getColumns(null, null, tableName, null); // retrieve all relevant column information for (int i = 0; backupRs.next(); i++) { Column column = new Column(backupRs.getString("COLUMN_NAME"), backupRs.getString("TYPE_NAME"), backupRs.getInt("COLUMN_SIZE")); columnList.add(column); } } } // create table object and add to the list of table definitions Table table = new Table(tableName, columnList); tableInfoList.add(table); } finally { if (rs != null) { rs.close(); } if (backupRs != null) { backupRs.close(); } } } return tableInfoList; } catch (Exception e) { throw new Exception("Could not retrieve database tables and columns.", e); } finally { if (connection != null) { connection.close(); } } } return null; }
From source file:com.mirth.connect.connectors.jdbc.DatabaseConnectorService.java
public Object invoke(String channelId, String method, Object object, String sessionsId) throws Exception { if (method.equals("getInformationSchema")) { // method 'getInformationSchema' will return Set<Table> Connection connection = null; try {//ww w. j av a 2s. c o m DatabaseConnectionInfo databaseConnectionInfo = (DatabaseConnectionInfo) object; String driver = databaseConnectionInfo.getDriver(); String address = replacer.replaceValues(databaseConnectionInfo.getUrl(), channelId); String user = replacer.replaceValues(databaseConnectionInfo.getUsername(), channelId); String password = replacer.replaceValues(databaseConnectionInfo.getPassword(), channelId); // Although these properties are not persisted, they used by the JdbcConnectorService String tableNamePatternExp = databaseConnectionInfo.getTableNamePatternExpression(); String selectLimit = databaseConnectionInfo.getSelectLimit(); String schema = null; Class.forName(driver); int oldLoginTimeout = DriverManager.getLoginTimeout(); DriverManager.setLoginTimeout(30); connection = DriverManager.getConnection(address, user, password); DriverManager.setLoginTimeout(oldLoginTimeout); DatabaseMetaData dbMetaData = connection.getMetaData(); // the sorted set to hold the table information SortedSet<Table> tableInfoList = new TreeSet<Table>(); // Use a schema if the user name matches one of the schemas. // Fix for Oracle: MIRTH-1045 ResultSet schemasResult = null; try { schemasResult = dbMetaData.getSchemas(); while (schemasResult.next()) { String schemaResult = schemasResult.getString(1); if (user.equalsIgnoreCase(schemaResult)) { schema = schemaResult; } } } finally { if (schemasResult != null) { schemasResult.close(); } } // based on the table name pattern, attempt to retrieve the table information List<String> tablePatternList = translateTableNamePatternExpression(tableNamePatternExp); List<String> tableNameList = new ArrayList<String>(); // go through each possible table name patterns and query for the tables for (String tableNamePattern : tablePatternList) { ResultSet rs = null; try { rs = dbMetaData.getTables(null, schema, tableNamePattern, TABLE_TYPES); // based on the result set, loop through to store the table name so it can be used to // retrieve the table's column information while (rs.next()) { tableNameList.add(rs.getString("TABLE_NAME")); } } finally { if (rs != null) { rs.close(); } } } // for each table, grab their column information for (String tableName : tableNameList) { ResultSet rs = null; ResultSet backupRs = null; boolean fallback = false; try { // apparently it's much more efficient to use ResultSetMetaData to retrieve // column information. So each driver is defined with their own unique SELECT // statement to query the table columns and use ResultSetMetaData to retrieve // the column information. If driver is not defined with the select statement // then we'll define to the generic method of getting column information, but // this could be extremely slow List<Column> columnList = new ArrayList<Column>(); if (StringUtils.isEmpty(selectLimit)) { logger.debug("No select limit is defined, using generic method"); rs = dbMetaData.getColumns(null, null, tableName, null); // retrieve all relevant column information for (int i = 0; rs.next(); i++) { Column column = new Column(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"), rs.getInt("COLUMN_SIZE")); columnList.add(column); } } else { logger.debug( "Select limit is defined, using specific select query : '" + selectLimit + "'"); // replace the '?' with the appropriate schema.table name, and use ResultSetMetaData to // retrieve column information final String schemaTableName = StringUtils.isNotEmpty(schema) ? schema + "." + tableName : tableName; final String queryString = selectLimit.trim().replaceAll("\\?", schemaTableName); Statement statement = connection.createStatement(); try { rs = statement.executeQuery(queryString); ResultSetMetaData rsmd = rs.getMetaData(); // retrieve all relevant column information for (int i = 1; i < rsmd.getColumnCount() + 1; i++) { Column column = new Column(rsmd.getColumnName(i), rsmd.getColumnTypeName(i), rsmd.getPrecision(i)); columnList.add(column); } } catch (SQLException sqle) { logger.info("Failed to execute '" + queryString + "', fall back to generic approach to retrieve column information"); fallback = true; } finally { if (statement != null) { statement.close(); } } // failed to use selectLimit method, so we need to fall back to generic // if this generic approach fails, then there's nothing we can do if (fallback) { // Re-initialize in case some columns were added before failing columnList = new ArrayList<Column>(); logger.debug("Using fallback method for retrieving columns"); backupRs = dbMetaData.getColumns(null, null, tableName, null); // retrieve all relevant column information for (int i = 0; backupRs.next(); i++) { Column column = new Column(backupRs.getString("COLUMN_NAME"), backupRs.getString("TYPE_NAME"), backupRs.getInt("COLUMN_SIZE")); columnList.add(column); } } } // create table object and add to the list of table definitions Table table = new Table(tableName, columnList); tableInfoList.add(table); } finally { if (rs != null) { rs.close(); } if (backupRs != null) { backupRs.close(); } } } return tableInfoList; } catch (Exception e) { throw new Exception("Could not retrieve database tables and columns.", e); } finally { if (connection != null) { connection.close(); } } } return null; }
From source file:com.clustercontrol.sql.util.AccessDB.java
/** * DB???/*from ww w. jav a 2 s.c o m*/ * * @throws SQLException * @throws ClassNotFoundException */ private void initial() throws SQLException, ClassNotFoundException { //JDBC?? try { Class.forName(m_jdbcDriver); } catch (ClassNotFoundException e) { m_log.info("initial() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); throw e; } Properties prop = jdbcProps.getProperties(); prop.put("user", m_user); prop.put("password", m_password); try { if (jdbcProps.isLoginTimeoutEnable()) { DriverManager.setLoginTimeout(jdbcProps.getLoginTimeout()); m_log.debug( "enabled loginTimeout (" + jdbcProps.getLoginTimeout() + " [sec]) for \"" + m_url + "\"."); } else { m_log.debug("disabled loginTimeout for \"" + m_url + "\"."); } m_connection = DriverManager.getConnection(m_url, prop); //SQL????Statement? m_statement = m_connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } catch (SQLException e) { m_log.info("initial() database access failure : url = " + m_url + ", : " + e.getClass().getSimpleName() + ", " + e.getMessage()); try { if (m_statement != null) m_statement.close(); } catch (SQLException se) { m_log.info("initial() database closing failure : url = " + m_url + ", " + se.getClass().getSimpleName() + ", " + se.getMessage()); } try { if (m_connection != null) m_connection.close(); } catch (SQLException se) { m_log.info("initial() database closing failure : url = " + m_url + ", " + se.getClass().getSimpleName() + ", " + se.getMessage()); } throw e; } }