📄 jahiaincrementorsdbbaseservice.java
字号:
//// JahiaIncrementorsDBBaseService// EV 02.11.2000//// autoIncrement()//package org.jahia.services.database;import java.sql.*; // ResultSetimport org.jahia.registries.*; // JahiaConsoleimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.exceptions.JahiaException;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;public class JahiaIncrementorsDBBaseService extends JahiaIncrementorsDBService { private String serviceName; private static JahiaIncrementorsDBBaseService theObject = null; /*** * constructor * EV 31.10.2000 * NK 24.12.2000 Client should always call getInstance() method instead */ protected JahiaIncrementorsDBBaseService() { JahiaConsole.println( "JahiaIncrementorsDBBaseService", "***** Starting the Jahia Incrementors DB Base Service *****" ); } // end constructor /*** * getInstance * EV 31.10.2000 * */ public static synchronized JahiaIncrementorsDBBaseService getInstance() { if (theObject == null) { theObject = new JahiaIncrementorsDBBaseService(); } return theObject; } // end getInstance /*** * autoIncrement * EV 02.11.2000 * */ public int autoIncrement( String tableName ) throws JahiaException { try { // gets the last id in the incrementors table int lastID = getLastID( tableName ); // adds the new id lastID++; updateLastID( tableName, lastID ); return lastID; } // catches sql error catch (JahiaException je) { JahiaConsole.println( "IncrementorsDBManager", "error in autoIncrement -> BAILING OUT" ); throw je; } } // end autoIncrement /*** * getLastID * EV 02.11.2000 * */ private int getLastID( String tableName ) throws JahiaException { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; int lastID = 0; try { int thereCanBeOnlyOne = 0; int current = 0; // composes the query String sqlQuery = ""; sqlQuery += "SELECT jahia_autoids_currentindex FROM jahia_autoids "; sqlQuery += "WHERE jahia_autoids_tablename='" + tableName + "'"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(35); // executes the query stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); // parses the results while (rs.next()) { current = rs.getInt( "jahia_autoids_currentindex") ; if (current > thereCanBeOnlyOne) { thereCanBeOnlyOne = current; } } // if no index was found, create a new one if (thereCanBeOnlyOne == 0) { createNewID( tableName ); } lastID = thereCanBeOnlyOne; } catch (SQLException se) { String errorMsg = "Error in finding last ID : " + se.getMessage(); JahiaConsole.println( "IncrementorsDBManager", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot access database incrementors", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); //if ( rs != null ) rs.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "getLastID : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } return lastID; } // end getLastID /*** * createNewID * EV 02.11.2000 * EV 04.11.2000 method is now synchrionized * */ private synchronized void createNewID( String tableName ) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // composes the query String sqlQuery = ""; sqlQuery += "INSERT INTO jahia_autoids (jahia_autoids_tablename, jahia_autoids_currentindex) "; sqlQuery += "VALUES('" + tableName + "',1)"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(36); // executes the query stmt = dbConn.createStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); JahiaConsole.println("JahiaIncrementorsDBBaseService.createNewID","Creating new ID for " + tableName ); } catch (SQLException se) { String errorMsg = "Error in createNewID : " + se.getMessage(); JahiaConsole.println( "IncrementorsDBManager", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot create new identifiers in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "createNewID : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end createNewID /*** * updateLastID * EV 02.11.2000 * EV 04.11.2000 method is now synchrionized * */ private synchronized void updateLastID( String tableName, int lastID ) throws JahiaException { Statement stmt = null; Connection dbConn = null; try { // composes the query String sqlQuery = ""; sqlQuery += "UPDATE jahia_autoids SET "; sqlQuery += "jahia_autoids_currentindex=" + lastID + " "; sqlQuery += "WHERE jahia_autoids_tablename='" + tableName + "'"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(37); // executes the query stmt = dbConn.createStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } catch (SQLException se) { String errorMsg = "Error in updateLastID : " + se.getMessage(); JahiaConsole.println( "IncrementorsDBManager", errorMsg ); JahiaConsole.println( "IncrementorsDBManager", "Cannot update auto-ids -> BAILING OUT" ); throw new JahiaException( "Cannot update identifiers in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "updateLastID : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end updateLastID /** * Method setName<br> * Set the name of the service * * @param String name */ public void setName(String name){ this.serviceName = name; } /*** * returns a DOM representation of the auto ids table * NK 13.08.2001 * */ public JahiaDOMObject getAutoIdsAsDOM() throws JahiaException{ Connection dbConn = null; Statement statement = null; ResultSet rs = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT * FROM jahia_autoids"; dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_autoids",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getAutoIdsAsDOM() : " + se.getMessage(); JahiaConsole.println( "JahiaIncrementorsDBBaseService", errorMsg); throw new JahiaException( "Cannot load data from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { rs.close(); rs = null; } catch ( Throwable t ) { } closeDBConnection (dbConn); closeStatement (statement); } return dom; } //------------------------------------------------------------------------- private Connection getDBConnection () { return getDBConnection(0); } //------------------------------------------------------------------------- private Connection getDBConnection (int debugInfo) { Connection dbConn = null; try { if ( debugInfo != 0 ){ dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (debugInfo); } else { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (); } } catch (NullPointerException ex) { JahiaConsole.println ("JahiaPagesDB", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("JahiaPagesDB", "SQL Exception: cannot get a connection."); } return dbConn; } //------------------------------------------------------------------------- private void closeDBConnection (Connection dbConn) { if (dbConn != null) { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection (dbConn); } catch (SQLException sqlEx) { // just create an exception without raising it, just to notify it // in the logs. JahiaException je = new JahiaException ("Cannot free resources", "Cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING); } catch (NullPointerException ex) { JahiaConsole.println ("JahiaPagesDB", "Null Pointer Exception, DB Pool Service instance might be null!"); } } } //------------------------------------------------------------------------- private void closeStatement (Statement statement) { // Close the opened statement try { if (statement!=null) { statement.close(); } } catch (SQLException sqlEx) { // just create an exception without raising it, just to notify it // in the logs. JahiaException je = new JahiaException ("Cannot close a statement", "Cannot close a statement", JahiaException.DATABASE_ERROR, JahiaException.WARNING); } }} // end JahiaIncrementorsDBBaseService
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -