本文整理汇总了Java中com.mysql.jdbc.CommunicationsException类的典型用法代码示例。如果您正苦于以下问题:Java CommunicationsException类的具体用法?Java CommunicationsException怎么用?Java CommunicationsException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CommunicationsException类属于com.mysql.jdbc包,在下文中一共展示了CommunicationsException类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: statusOnException
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
public int statusOnException(Connection arg0, Throwable throwable) {
if (throwable instanceof CommunicationsException || "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException".equals(throwable.getClass().getName())) {
return CONNECTION_IS_INVALID;
}
if (throwable instanceof SQLException) {
String sqlState = ((SQLException) throwable).getSQLState();
if (sqlState != null && sqlState.startsWith("08")) {
return CONNECTION_IS_INVALID;
}
return CONNECTION_IS_OKAY;
}
// Runtime/Unchecked?
return CONNECTION_IS_INVALID;
}
示例2: executeSQLQueryWithFetching
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
/**
* Executes a given SQL query as String using a generic statement.
* Fetch resultset row by row (cf. MySQL JDBC driver doc).
* As it returns a ResultSet, this statement needs to be explicitly closed after the processing of the ResultSet with function
* {see closeTableStatement()}.
* Attention, no other use of the connection must be done before the generic table statement to be closed.
*/
protected ResultSet executeSQLQueryWithFetching(String query) throws SQLException {
ResultSet rSet;
try {
tableStatement = tableConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
tableStatement.setFetchSize(Integer.MIN_VALUE);
rSet = tableStatement.executeQuery(query);
} catch (CommunicationsException e) {
reOpenConnectionIfClosed();
tableStatement = tableConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
tableStatement.setFetchSize(Integer.MIN_VALUE);
rSet = tableStatement.executeQuery(query);
}
//logger.info("query: " + query);
return rSet;
}
示例3: statusOnException
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
public int statusOnException(Connection arg0, Throwable throwable) {
if (throwable instanceof CommunicationsException
|| "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException"
.equals(throwable.getClass().getName())) {
return CONNECTION_IS_INVALID;
}
if (throwable instanceof SQLException) {
String sqlState = ((SQLException) throwable).getSQLState();
if (sqlState != null && sqlState.startsWith("08")) {
return CONNECTION_IS_INVALID;
}
return CONNECTION_IS_OKAY;
}
// Runtime/Unchecked?
return CONNECTION_IS_INVALID;
}
示例4: executeSQLQuery
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
/**
* Executes a given SQL query as String using a generic statement. As it returns a ResultSet,
* this statement needs to be explicitly closed after the processing of the ResultSet with function
* {see closeTableStatement()}.
*/
protected ResultSet executeSQLQuery(String query) throws SQLException {
ResultSet rSet;
try {
tableStatement = tableConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rSet = tableStatement.executeQuery(query);
} catch (CommunicationsException e) {
reOpenConnectionIfClosed();
tableStatement = tableConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rSet = tableStatement.executeQuery(query);
}
//logger.info("query: " + query);
return rSet;
}
示例5: jbtncleandataActionPerformed
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
private void jbtncleandataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtncleandataActionPerformed
new Thread() {
public void run() {
try {
Properties config = new Properties();
config.load(new FileInputStream(System.getProperty("user.dir") + "//config//config.properties"));
Class.forName(config.getProperty("dbdriver"));
Connection conn = DriverManager.getConnection(config.getProperty("dbstring"), config.getProperty("dbuser"), config.getProperty("dbpass"));
int a = JOptionPane.showConfirmDialog(null, "Are you sure to clean data ?", "Log Analyzer", JOptionPane.YES_NO_OPTION);
if (a == JOptionPane.YES_OPTION) {
jlblstatus.setText("Cleaning data...");
jbtncleandata.setEnabled(false);
PreparedStatement ps = conn.prepareStatement("delete from tbl_http_log");
int n = ps.executeUpdate();
if (n > 0) {
JOptionPane.showMessageDialog(null, "Data successfully cleaned", "Log Analyzer", JOptionPane.INFORMATION_MESSAGE);
jlblstatus.setText("");
}
}
} catch (CommunicationsException ce) {
System.out.println(ce.getMessage());
} catch (Exception e) {
System.out.println("Erro - " + e);
}
}
}.start();
}
示例6: testBug74998
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
/**
* Tests fix for BUG#74998 - readRemainingMultiPackets not computed correctly for rows larger than 16 MB.
*
* This bug is observed only when a multipacket uses packets 127 and 128. It happens due to the transition from positive to negative values in a signed byte
* numeric value (127 + 1 == -128).
*
* The test case forces a multipacket to use packets 127, 128 and 129, where packet 129 is 0-length, this being another boundary case.
* Query (*1) generates the following MySQL protocol packets from the server:
* - Packets 1 to 4 contain protocol control data and results metadata info. (*2)
* - Packets 5 to 126 contain each row "X". (*3)
* - Packets 127 to 129 contain row "Y..." as a multipacket (size("Y...") = 32*1024*1024-15 requires 3 packets). (*4)
* - Packet 130 contains row "Z". (*5)
*
* @throws Exception
* if the test fails.
*/
public void testBug74998() throws Exception {
int maxAllowedPacketAtServer = Integer.parseInt(((MySQLConnection) this.conn).getServerVariable("max_allowed_packet"));
int maxAllowedPacketMinimumForTest = 32 * 1024 * 1024;
if (maxAllowedPacketAtServer < maxAllowedPacketMinimumForTest) {
fail("You need to increase max_allowed_packet to at least " + maxAllowedPacketMinimumForTest + " before running this test!");
}
createTable("testBug74998", "(id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, data LONGBLOB)"); // (*2)
StringBuilder query = new StringBuilder("INSERT INTO testBug74998 (data) VALUES ('X')");
for (int i = 0; i < 121; i++) {
query.append(",('X')");
}
assertEquals(122, this.stmt.executeUpdate(query.toString())); // (*3)
int lengthOfRowForMultiPacket = maxAllowedPacketMinimumForTest - 15; // 32MB - 15Bytes causes an empty packet at the end of the multipacket sequence
this.stmt.executeUpdate("INSERT INTO testBug74998 (data) VALUES (REPEAT('Y', " + lengthOfRowForMultiPacket + "))"); // (*4)
this.stmt.executeUpdate("INSERT INTO testBug74998 (data) VALUES ('Z')"); // (*5)
try {
this.rs = this.stmt.executeQuery("SELECT id, data FROM testBug74998 ORDER BY id"); // (*1)
} catch (CommunicationsException e) {
if (e.getCause() instanceof IOException && "Packets received out of order".compareTo(e.getCause().getMessage()) == 0) {
fail("Failed to correctly fetch all data from communications layer due to wrong processing of muli-packet number.");
} else {
throw e;
}
}
// safety check
for (int i = 1; i <= 122; i++) {
assertTrue(this.rs.next());
assertEquals(i, this.rs.getInt(1));
assertEquals("X", this.rs.getString(2));
}
assertTrue(this.rs.next());
assertEquals(123, this.rs.getInt(1));
assertEquals("YYYYY", this.rs.getString(2).substring(0, 5));
assertEquals("YYYYY", this.rs.getString(2).substring(lengthOfRowForMultiPacket - 5));
assertTrue(this.rs.next());
assertEquals(124, this.rs.getInt(1));
assertEquals("Z", this.rs.getString(2));
assertFalse(this.rs.next());
}
示例7: testConnectionHibernate
import com.mysql.jdbc.CommunicationsException; //导入依赖的package包/类
/**
* This method uses Hibernates SessionFactory to get a Session and using Hibernates Criteria does a sample query
* to connection and obtain results as part of testing for successful connection.
*
* Based on the kind of exceptions this method throws CSException with appropriate message.
* @param appForm -
* The ApplicationForm with application database parameters to
* test connection for.
* @return String - The message indicating that connection and a SQL query
* was successful
* @throws CSException -
* The exception message indicates which kind of application
* database parameters are invalid.
*/
public static String testConnectionHibernate(BaseDBForm appForm) throws CSException {
ApplicationForm appform = (ApplicationForm) appForm;
SessionFactory sf = null;
try {
Configuration configuration = new Configuration();
configuration.addResource("gov/nih/nci/security/authorization/domainobjects/Application.hbm.xml");
configuration.setProperty("hibernate.connection.url",appform.getApplicationDatabaseURL());
configuration.setProperty("hibernate.connection.username",appform.getApplicationDatabaseUserName());
configuration.setProperty("hibernate.connection.password",appform.getApplicationDatabasePassword());
configuration.setProperty("hibernate.dialect",appform.getApplicationDatabaseDialect());
configuration.setProperty("hibernate.connection.driver_class",appform.getApplicationDatabaseDriver());
configuration.setProperty("hibernate.connection.pool_size","1");
sf = configuration.buildSessionFactory();
Session session = sf.openSession();
Criteria criteria = session.createCriteria(ApplicationContext.class);
criteria.add(Expression.eq("applicationName", appform.getApplicationName().trim()));
criteria.setProjection(Projections.rowCount());
List results = criteria.list();
Integer integer = (Integer) results.iterator().next();
if (integer == null) {
session.close();
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED);
}
session.close();
return DisplayConstants.APPLICATION_DATABASE_CONNECTION_SUCCESSFUL;
} catch(Throwable t){
// Depending on the cause of the exception obtain message and throw a CSException.
if(t instanceof SQLGrammarException){
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED + " "+t.getCause().getMessage());
}
if(t instanceof JDBCConnectionException){
if(t.getCause() instanceof CommunicationsException){
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL_SERVER_PORT);
}
if(t.getCause() instanceof SQLException){
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL);
}
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED+" "+t.getMessage());
}
if(t instanceof GenericJDBCException){
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL_USER_PASS);
}
if(t instanceof HibernateException){
throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED+" "+t.getMessage());
}
throw new CSException(
DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED);
}
}