dbpersistencemanager.java

来自「JGRoups源码」· Java 代码 · 共 688 行 · 第 1/2 页

JAVA
688
字号
        Statement stat=null;        try {            conn=this.getConnection();            stat=conn.createStatement();            stat.executeQuery("delete from replhashmap");        }        catch(Throwable t) {            //trace here            throw new CannotRemoveException(t, " delete all query failed with existing database");        }        //finally        try {            stat.close();            this.closeConnection(conn);        }        catch(Throwable t) {            conn=null;            stat=null;        }    }    /**     * Shutting down the database cleanly     */    public void shutDown() {        // non-trivial problem, more research required        // no-op for now..    }    /**     * The private interfaces are used specifically to this manager     */    /**     * Used to enter a completely new row in to the current table     * @param Serializable; key     * @param Serializable; value     * @exception CannotPersistException;     */    private void addNewEntry(Serializable key, Serializable val) throws CannotPersistException, CannotConnectException {        Connection conn=getConnection();        try {            PreparedStatement prepStat=conn.prepareStatement(insertStat);            prepStat.setString(1, key.toString());            byte[] keyBytes=getBytes(key);            byte[] valBytes=getBytes(val);            //InputStream keyStream = getBinaryInputStream(key);            //InputStream valStream = getBinaryInputStream(val);            prepStat.setBytes(2, keyBytes);            prepStat.setBytes(3, valBytes);            //prepStat.setBinaryStream(keyStream);            //prepStat.setBinaryStream(valStream);            prepStat.executeQuery();            conn.commit();            log.error(" executing insert " + insertStat);        }        catch(Throwable t) {            //conn.rollback();            t.printStackTrace();            //trace here            throw new CannotPersistException(t, "error adding new entry using creating Db connection and schema");        }    }// end of addentry..    /**     * Gets a binaryinputstream from a serialized object     * @param Serializable;     * @return BinaryInputStream;     * @exception Exception;     */    private java.io.InputStream getBinaryInputStream(Serializable ser) throws Exception {        ByteArrayOutputStream stream=new ByteArrayOutputStream();        ObjectOutputStream keyoos=new ObjectOutputStream(stream);        keyoos.writeObject(ser);        ByteArrayInputStream pipe=new ByteArrayInputStream(stream.toByteArray());        return pipe;    }// end of stream conversion    /**     * Gets a serializable back from a InputStream     * @param InputStream;     * @return Serializable;     * @exception Exception;     */    private Serializable getSerializable(java.io.InputStream stream) throws Exception {        ObjectInputStream ooStr=new ObjectInputStream(stream);        Serializable tmp=(Serializable) ooStr.readObject();        return tmp;    }    /**     * Used to enter a completely new row in to the current table     * @param Serializable; key     * @param Serializable; value     * @exception CannotPersistException;     */    private void addNewEntryGen(Serializable key, Serializable val) throws CannotPersistException, CannotConnectException {        Connection conn=getConnection();        try {            PreparedStatement prepStat=conn.prepareStatement(insertStat);            prepStat.setString(1, key.toString());            prepStat.setBytes(2, getBytes(key));            prepStat.setBytes(3, getBytes(val));            prepStat.executeUpdate();        }        catch(Throwable t) {            //trace here            throw new CannotPersistException(t, "error adding new entry using creating Db connection and schema");        }    }// end of entering new row gen    /**     * Used to enter a completely new row in to the current table     * @param Serializable; key     * @param Serializable; value     * @exception CannotPersistException;     */    private void addNewEntryOra(Serializable key, Serializable val) throws CannotPersistException, CannotConnectException {        Connection conn=getConnection();        try {            PreparedStatement prepStat=conn.prepareStatement(insertStat);            prepStat.setString(1, key.toString());            InputStream keyBin=getBinaryInputStream(key);            InputStream keyVal=getBinaryInputStream(val);            byte[] keyBytes=getBytes(key);            byte[] valBytes=getBytes(val);            prepStat.setBytes(2, keyBytes);            prepStat.setBytes(3, valBytes);            prepStat.executeBatch();        }        catch(Throwable t) {            //trace here            throw new CannotPersistException(t, "error adding new entry using creating Db connection and schema");        }    }// end of entering new row ora    /**     * Cache checking     * @param java.io.Serializable     * @return boolean;     */    private boolean entryExists(Serializable key) {        return list.contains(key.toString());    }    /**     * Conversion helper     * @param Serializable;     * @return byte[];     */    private byte[] getBytes(Serializable ser) throws Exception {        ByteArrayOutputStream stream=new ByteArrayOutputStream();        ObjectOutputStream keyoos=new ObjectOutputStream(stream);        keyoos.writeObject(ser);        byte[] keyBytes=stream.toByteArray();        return keyBytes;    }// end of getBytes    /**     * ALL IMPL below is for INIT purposes     */    /**     * This method will be invoked by defauly by each persistence     * manager to read from a default location or one provided by     * the caller.     * @return void;     * @exception Exception;     */    private void readProps(String filePath) throws Exception {        FileInputStream _stream=new FileInputStream(filePath);        props=new Properties();        props.load(_stream);        // using properties to set most used variables        driverName=props.getProperty("jdbc.Driver");        connStr=props.getProperty("jdbc.Conn").trim();        userName=props.getProperty("jdbc.User").trim();        userPass=props.getProperty("jdbc.Pass").trim();        createTable=props.getProperty("jdbc.table").trim();    }    /**     * Duplicate reader using stream instead of dile     * @param InputStream;     * @exception Exception;     */    private void readProps(InputStream input) throws Exception {        props=new Properties();        props.load(input);        // using properties to set most used variables        driverName=props.getProperty("jdbc.Driver");        connStr=props.getProperty("jdbc.Conn");        userName=props.getProperty("jdbc.User");        userPass=props.getProperty("jdbc.Pass");        createTable=props.getProperty("jdbc.table");    }    /**     * Loads the driver using the driver class name. Drivers can be simply     * loaded by loading the class or by registering specifically using the     * JDBC DriverManager     * @return void;     * @exception Exception;     */    private void loadDriver() throws Exception {        // driver classes when loaded load the driver into VM        Class.forName(driverName);    }    /**     * Once the driver is loaded, the DB is ready to be connected. This     * method provides a handle to connect to the DB.     * @return Connection;     * @exception CannotConnectException;     */    private Connection getConnection() throws CannotConnectException {        try {            connStr=connStr.trim();            Connection conn=DriverManager.getConnection(connStr, userName, userPass);            if(log.isInfoEnabled()) log.info("userName=" + userName +                                             ", userPass=" + userPass + ", connStr=" + connStr);            return conn;        }        catch(Throwable t) {            t.printStackTrace();            //trace here            throw new CannotConnectException(t, "Error in creating connection using provided properties ");        }    }// end of get conn..    /**     * Method is used for closing created connection.     * Pooling is not implemented currently, but will be made available     * as soon as this manager uses large number of transactions     * @param Connection     */    private void closeConnection(Connection conn) {        try {            if(conn != null) {                conn.close();                conn=null;            }        }        catch(Throwable t) {            //trace here            conn=null;        }    }// end of closeConn    /**     * Used to create table provided the DB instance     * @exception CannotCreateSchemaException;     * @exception CannotConnectException;     */    private void createDBTables() throws CannotCreateSchemaException, CannotConnectException {        Connection conn=this.getConnection();        Statement stat=null;        try {            stat=conn.createStatement();        }        catch(Exception e) {            //trace here..            e.printStackTrace();            throw new CannotConnectException(e, "there was an error in creating statements for persisting data using created connection");        }        try {            ResultSet set=stat.executeQuery("select * from replhashmap");        }        catch(Throwable t) {            t.printStackTrace();            //use connection to create new statement            addSchemaToDB(conn);        }// end of out throwable..    }// end of method..    /**     * used to create required table within the DB     * @param Connection;     * @exception CannotCreateSchema;     */    private void addSchemaToDB(Connection conn) throws CannotCreateSchemaException {        Statement stat=null;        Statement stat2=null;        try {            stat=conn.createStatement();            log.error(" executing query for oracle " + createTable);            stat.executeQuery(createTable);        }        catch(Throwable t) {            t.printStackTrace();            // trace here            throw new CannotCreateSchemaException(t, "error was using schema with blobs");        }// end of catch                // clean up is required after init        finally {            try {                if(stat != null) stat.close();                this.closeConnection(conn);            }            catch(Throwable t3) {            }        }// end of finally..    }// end of gen schema..    private Properties props=null;    private String driverName=null;    private String userName=null;    private String userPass=null;    private String connStr=null;    private String createTable=null;    private final boolean oracleDB=false;    private Vector list=null;    private static final String tabName="replhashmap";    private static final String insertStat="insert into replhashmap(key, keyBin, valBin) values  (?, ?, ?)";    private static final String updateStat="update replhashmap set keyBin = ?, valBin = ? where key like ?";    private static final String removeStat=" delete from replhashmap where key like ?";    private static final String createTableGen=" create table replhashmap(key varchar, keyBin varbinary, valBin varbinary)";    private static final String createTableOra=" create table replhashmap ( key varchar2(100), keyBin blob, valBin blob)";}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?