Java Code Examples for com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

The following examples show how to use com.mysql.jdbc.exceptions.jdbc4.CommunicationsException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public Connection getConnection() {
	try {
		if(sqlite){
			// To avoid concurrency problem with SQLite, we will just use one connection. Cf : constructor above for SQLite
			synchronized (SQLiteConn) {
				SQLiteConn = DriverManager.getConnection("jdbc:sqlite:" + BAT.getInstance().getDataFolder().getAbsolutePath() + File.separator
							+ "bat_database.db");
				return SQLiteConn;
			}
		}
		return ds.getConnection();
	} catch (final SQLException e) {
		BAT.getInstance().getLogger().severe(
		        "BAT can't etablish connection with the database. Please report this and include the following lines :");
		if(e.getCause() instanceof CommunicationsException){
		    BAT.getInstance().getLogger().severe(e.getCause().getMessage());
		}
           if (BAT.getInstance().getConfiguration().isDebugMode()) {
               e.printStackTrace();
           }
		return null;
	}
}
 
Example 2
Source Project: pinlater   Source File: MySQLBackendUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Indicates whether an exception is a symptom of MySQL being overloaded or slow.
 * Typical symptoms are connection pool exhaustion or MySQL connection/socket timeouts.
 */
public static boolean isExceptionIndicativeOfOverload(Exception e) {
  return e instanceof MySQLNonTransientConnectionException
      || e instanceof CommunicationsException
      || (e instanceof SQLNestedException
              && e.getCause() instanceof NoSuchElementException);
}
 
Example 3
/**
 * Constructor used for MySQL
 * 
 * @param host
 * @param port
 * @param database
 * @param username
 * @param password
 * @throws SQLException 
 */
public DataSourceHandler(final String host, final String port, final String database, final String username, final String password) throws SQLException{
	// Check database's informations and init connection
	this.host = Preconditions.checkNotNull(host);
	this.port = Preconditions.checkNotNull(port);
	this.database = Preconditions.checkNotNull(database);
	this.username = Preconditions.checkNotNull(username);
	this.password = Preconditions.checkNotNull(password);

	BAT.getInstance().getLogger().config("Initialization of HikariCP in progress ...");
	BasicConfigurator.configure(new NullAppender());
	ds = new HikariDataSource();
	ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + 
			"?useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID());
	ds.setUsername(this.username);
	ds.setPassword(this.password);
	ds.addDataSourceProperty("cachePrepStmts", "true");
	ds.setMaximumPoolSize(8);
	try {
		final Connection conn = ds.getConnection();
	    int intOffset = Calendar.getInstance().getTimeZone().getOffset(Calendar.getInstance().getTimeInMillis()) / 1000;
	    String offset = String.format("%02d:%02d", Math.abs(intOffset / 3600), Math.abs((intOffset / 60) % 60));
	    offset = (intOffset >= 0 ? "+" : "-") + offset;
		conn.createStatement().executeQuery("SET time_zone='" + offset + "';");
		conn.close();
		BAT.getInstance().getLogger().config("BoneCP is loaded !");
	} catch (final SQLException e) {
		BAT.getInstance().getLogger().severe("BAT encounters a problem during the initialization of the database connection."
				+ " Please check your logins and database configuration.");
		if(e.getCause() instanceof CommunicationsException){
		    BAT.getInstance().getLogger().severe(e.getCause().getMessage());
		}
		if(BAT.getInstance().getConfiguration().isDebugMode()){
		    BAT.getInstance().getLogger().log(Level.SEVERE, e.getMessage(), e);
		}
		throw e;
	}
	sqlite = false;
}