static String driverName = "org.gjt.mm.mysql.Driver";
//static String jdbcPath = "@jdbc.path@";
static String jdbcPath = "jdbc:mysql://localhost/tpcw";
private static final boolean use_connection_pool = true; public static synchronized Connection getConnection() {
if (!use_connection_pool) {
return getNewConnection();
} else {
Connection con = null;
while (availConn.size() > 0) {
// Pick the first Connection in the Vector
// to get round-robin usage
con = (Connection) availConn.firstElement();
availConn.removeElementAt(0);
try {
if (con.isClosed()) {
continue;
}
}
catch (SQLException e) {
e.printStackTrace();
continue;
} // Got a connection.
checkedOut++;
return(con);
} if (maxConn == 0 || checkedOut < maxConn) {
con = getNewConnection();
totalConnections++;
}
if (con != null) {
checkedOut++;
} return con;
}
} // Return a connection to the pool.
public static synchronized void returnConnection(Connection con)
throws java.sql.SQLException
{
if (!use_connection_pool) {
con.close();
} else {
checkedOut--;
availConn.addElement(con);
}
} // Get a new connection to mysql
public static Connection getNewConnection() {
try {
Class.forName(driverName);
// Class.forName("postgresql.Driver"); // Create URL for specifying a DBMS
Connection con;
while(true) {
try {
con = DriverManager.getConnection(jdbcPath);
break;
} catch (java.sql.SQLException ex) {
System.err.println("Error getting connection: " +
ex.getMessage() + " : " +
ex.getErrorCode() +
": trying to get connection again.");
ex.printStackTrace();
java.lang.Thread.sleep(1000);
}
}
con.setAutoCommit(false);
createdConnections++;
return con;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return null;
}
//static String jdbcPath = "@jdbc.path@";
static String jdbcPath = "jdbc:mysql://localhost/tpcw";
private static final boolean use_connection_pool = true; public static synchronized Connection getConnection() {
if (!use_connection_pool) {
return getNewConnection();
} else {
Connection con = null;
while (availConn.size() > 0) {
// Pick the first Connection in the Vector
// to get round-robin usage
con = (Connection) availConn.firstElement();
availConn.removeElementAt(0);
try {
if (con.isClosed()) {
continue;
}
}
catch (SQLException e) {
e.printStackTrace();
continue;
} // Got a connection.
checkedOut++;
return(con);
} if (maxConn == 0 || checkedOut < maxConn) {
con = getNewConnection();
totalConnections++;
}
if (con != null) {
checkedOut++;
} return con;
}
} // Return a connection to the pool.
public static synchronized void returnConnection(Connection con)
throws java.sql.SQLException
{
if (!use_connection_pool) {
con.close();
} else {
checkedOut--;
availConn.addElement(con);
}
} // Get a new connection to mysql
public static Connection getNewConnection() {
try {
Class.forName(driverName);
// Class.forName("postgresql.Driver"); // Create URL for specifying a DBMS
Connection con;
while(true) {
try {
con = DriverManager.getConnection(jdbcPath);
break;
} catch (java.sql.SQLException ex) {
System.err.println("Error getting connection: " +
ex.getMessage() + " : " +
ex.getErrorCode() +
": trying to get connection again.");
ex.printStackTrace();
java.lang.Thread.sleep(1000);
}
}
con.setAutoCommit(false);
createdConnections++;
return con;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return null;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货