/**
* @author ivan
*
*/
public class ReadProperties {
/*
* , Properties
* (non-Javadoc)
* @see com.voole.interf.IProperties#getProperties()
* @return Properties
*/
public static Properties getProperties() {
return getProperties("/config.properties");
}
public static Properties getProperties(String path) {
InputStream inStream = ReadProperties.class.getResourceAsStream(path);
Properties prop = new Properties();
try {
if(inStream != null){
prop.load(inStream);
inStream.close();
}
} catch (IOException e) {
System.out.println(" !"+e.getMessage());
}
return prop;
}
//
public static void main(String[] args) {
Properties pr = ReadProperties.getProperties();
System.out.println("mysql connection -- " + pr.getProperty("SQL_DRIVER"));
}
}
public final class ConnectionManager {
private ComboPooledDataSource ds;
private ConnectionManager(String propertiesPath) throws Exception {
ds = new ComboPooledDataSource();
Properties properties = ReadProperties.getProperties(propertiesPath);
// ds.setDriverClass("oracle.jdbc.driver.OracleDriver");
// ds.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
// ds.setUser("test");
// ds.setPassword("testtest");
ds.setDriverClass(properties.getProperty("SQL_DRIVER"));
ds.setJdbcUrl(properties.getProperty("CONTEST_CONN_URL"));
// , minPoolSize maxPoolSize 。Default: 3 initialPoolSize
ds.setInitialPoolSize(3);
// 。Default: 15 maxPoolSize
ds.setMaxPoolSize(50);
//// 。
//ds.setMinPoolSize(1);
// c3p0 。Default: 3 acquireIncrement
ds.setAcquireIncrement(1);
// 60 。Default: 0 idleConnectionTestPeriod
ds.setIdleConnectionTestPeriod(60);
// ,25000 。 0 。Default: 0 maxIdleTime
ds.setMaxIdleTime(25000);
// 。Default: false autoCommitOnClose
ds.setAutoCommitOnClose(true);
// 。 。 :
// 。Default: null preferredTestQuery
ds.setPreferredTestQuery("select now()");
// 。 true connection
// 。 idleConnectionTestPeriod automaticTestTable
// 。Default: false testConnectionOnCheckout
ds.setTestConnectionOnCheckout(false);
// true 。Default: false testConnectionOnCheckin
ds.setTestConnectionOnCheckin(true);
// 。Default: 30 acquireRetryAttempts
ds.setAcquireRetryAttempts(30);
// , 。Default: 1000 acquireRetryDelay
ds.setAcquireRetryDelay(1000);
// 。
// , getConnection() 。 true,
// 。Default: false breakAfterAcquireFailure
ds.setBreakAfterAcquireFailure(true);
// <!-- getConnection() ,
// SQLException, 0 。 。Default: 0 -->
// <property name="checkoutTimeout">100</property>
// <!--c3p0 Test , 。
// preferredTestQuery 。 Test , c3p0
// 。Default: null-->
// <property name="automaticTestTable">Test</property>
// <!--JDBC , PreparedStatements 。 statements
// connection 。 。
// maxStatements maxStatementsPerConnection 0, 。Default: 0-->
// <property name="maxStatements">100</property>
// <!--maxStatementsPerConnection statements 。Default: 0 -->
// <property name="maxStatementsPerConnection"></property>
// <!--c3p0 , JDBC 。
// 。Default: 3-->
// <property name="numHelperThreads">3</property>
// <!-- 300 。Default: 300 -->
// <property name="propertyCycle">300</property>
}
private static Map<String , ConnectionManager> managers = new HashMap<String, ConnectionManager>();
public static synchronized ConnectionManager getConnectionManager(String propertiesPath){
ConnectionManager manager = managers.get(propertiesPath);
if(manager == null){
try {
manager = new ConnectionManager(propertiesPath);
managers.put(propertiesPath, manager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return manager;
}
public Connection getConnection() throws SQLException {
return ds.getConnection();
}
protected void finalize() throws Throwable {
DataSources.destroy(ds); // datasource
super.finalize();
}
public static void closeConnection(Connection c){
try {
if(c != null)
c.close();
} catch (SQLException e) {
}
}
public static void closeStatement(Statement c){
try {
if(c != null)
c.close();
} catch (SQLException e) {
}
}
public static void closeResultSet(ResultSet c){
try {
if(c != null)
c.close();
} catch (SQLException e) {
}
}
}
public class DB {
public static Connection getConnection(String sqlDriver, String url) {
Connection conn = null;
try {
Class.forName(sqlDriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
try {
Thread.sleep(5000);
getConnection(sqlDriver, url);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return conn;
}
// c3p0 connection
public static Connection getConnection(String propertyName){
Connection conn = null;
ConnectionManager manager = ConnectionManager.getConnectionManager(propertyName);
try {
conn = manager.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static PreparedStatement getPrepaStatement (Connection conn,String sql) {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(Statement stmt,String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void update(Statement stmt,String sql){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeConnection(Connection conn){
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeStatement(Statement stmt){
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public void updateIndex() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = manager.getConnection();
stmt = DB.getStatement(conn);
String sqlSum = "select count(*) from IndexServiceCompany where ExecStatus=1";
rs = DB.getResultSet(stmt, sqlSum);
int recordSum = 0;
while (rs.next()) {
recordSum = rs.getInt(1);
}
int createTotal = 0;
if (recordSum > 0) {
createTotal = recordSum / indexSize + 1; //
for (int j = 0; j < createTotal; j++) {
String sql = "select TOP ("
+ indexSize
+ ")* from IndexServiceCompany where ExecStatus=1 order by IID ";
String update = "";
rs = DB.getResultSet(stmt, sql);
int status = 0;
while (rs.next()) {
iid = rs.getInt("IID");
status = rs.getInt("EditStatus");
switch (status) {
case 1:
update(conn, rs.getString("TableName"), rs
.getString("ObjectCode"));//
System.out.println(" ...");
break;
case 2:
update(conn, rs.getString("TableName"), rs
.getString("ObjectCode"));
System.out.println(" ...");
break;
case 3:
delete(rs.getString("ObjectCode"));
default:
break;
}
update += iid + ",";
}
// docs solr
if (addDocs.size() > 0) {
solrServer.add(addDocs);
solrServer.commit();
System.out.println(" :" + addDocs.size() + " ");
addDocs.clear();
}
// , solr
update = update.substring(0, update.length() - 1);
String upSql = "update IndexServiceCompany set ExecStatus=2, ExecTime=getdate() where IID in ("
+ update + ")";
DB.update(stmt, upSql);
System.out
.println(new Date() + " -- " + (j + 1) + " OK");
}// for over
} else {
System.out.println(new Date() + " -- no data ...");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
ConnectionManager.closeConnection(conn);
}
}