details: https://code.openbravo.com/erp/devel/pi/rev/a2cba155ca8f changeset: 33336:a2cba155ca8f user: Augusto Mauch <augusto.mauch <at> openbravo.com> date: Fri Jan 19 11:58:05 2018 +0100 summary: Fixes issue 37677: Dont check if ad_context_info table exists before creating it
Now, if the pool is used, the query to check if the ad_context_info table already exists will not be done. This check can be skipped because if the pool is used, the initDB method will only be called when a new physical connection is created, and in that case the temporary table is garanteed not to exist. This pool instance check is needed to keep supporting using pools other than org.openbravo.apachejdbcconnectionpool.JdbcExternalConnectionPool (or none at al), but in those cases the query to check if the ad_context_info table exists will still be performed. details: https://code.openbravo.com/erp/devel/pi/rev/b42f32f0e0e9 changeset: 33337:b42f32f0e0e9 user: Augusto Mauch <augusto.mauch <at> openbravo.com> date: Fri Jan 19 13:42:17 2018 +0100 summary: Fixes issue 37681: Dont create ad_context_info if audit trail not active Records are only read/written from the ad_context_info if audit trail is enabled (if any ad_table is fully audited). If audit trail is disabled when Tomcat is started, there is no need to create the ad_context_info table each time that a new connection is created. From now on it will no be possible to enable the audit trail while Tomcat is active, enabling it will require to rebuild Openbravo and to restart Tomcat. Now: - The Update Audit Trail Infrastructure has been removed - When a new table is set to be audited in the AD, SessionInfo.setAuditTrailActive(true) will not be invoked, so that the isAuditTrailActive flag can only be set to true when Tomcat starts. diffstat: modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java | 26 +- src-core/src/org/openbravo/database/SessionInfo.java | 110 ++++- src-db/database/sourcedata/AD_MENU.xml | 14 - src-db/database/sourcedata/AD_MESSAGE.xml | 4 +- src-db/database/sourcedata/AD_MODEL_OBJECT.xml | 12 - src-db/database/sourcedata/AD_PROCESS.xml | 27 - src-db/database/sourcedata/AD_TREENODE.xml | 11 - src/org/openbravo/erpCommon/ad_callouts/SL_TableAudit.java | 12 +- src/org/openbravo/erpCommon/ad_process/UpdateAuditTrail.java | 162 ---------- 9 files changed, 109 insertions(+), 269 deletions(-) diffs (truncated from 538 to 300 lines): diff -r 28f74a227054 -r b42f32f0e0e9 modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java --- a/modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java Thu Feb 01 11:02:05 2018 +0000 +++ b/modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java Fri Jan 19 13:42:17 2018 +0100 @@ -38,7 +38,8 @@ public class ConnectionInitializerInterceptor extends JdbcInterceptor implements PoolInterceptorProvider { - private static final String INITIALIZED = "OB_INITIALIZED"; + private static final String SESSION_CONFIG_APPLIED = "OB_INITIALIZED"; + private static final String SESSION_INFO_APPLIED = "SESSION_INFO_INITIALIZED"; String rbdms = (String) OBPropertiesProvider.getInstance().getOpenbravoProperties() .get("bbdd.rdbms"); @@ -51,11 +52,8 @@ public void reset(ConnectionPool parent, PooledConnection con) { if (con != null) { HashMap<Object, Object> attributes = con.getAttributes(); - Boolean connectionInitialized = (Boolean) attributes.get(INITIALIZED); - if (connectionInitialized == null || connectionInitialized == false) { - if (!isReadOnlyPool(parent)) { - SessionInfo.initDB(con.getConnection(), rbdms); - } + Boolean sessionInfoApplied = (Boolean) attributes.get(SESSION_CONFIG_APPLIED); + if (sessionInfoApplied == null || !sessionInfoApplied) { PreparedStatement pstmt = null; try { final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties(); @@ -73,7 +71,21 @@ throw new OBException(e); } } - attributes.put(INITIALIZED, true); + attributes.put(SESSION_CONFIG_APPLIED, true); + } + + Boolean sessionInfoInitialized = (Boolean) attributes.get(SESSION_INFO_APPLIED); + if (sessionInfoInitialized == null || !sessionInfoInitialized) { + boolean initialized = false; + if (isReadOnlyPool(parent)) { + initialized = true; + } else { + if (SessionInfo.isInitialized()) { + SessionInfo.initDB(con.getConnection(), rbdms); + initialized = true; + } + } + attributes.put(SESSION_INFO_APPLIED, initialized); } } } diff -r 28f74a227054 -r b42f32f0e0e9 src-core/src/org/openbravo/database/SessionInfo.java --- a/src-core/src/org/openbravo/database/SessionInfo.java Thu Feb 01 11:02:05 2018 +0000 +++ b/src-core/src/org/openbravo/database/SessionInfo.java Fri Jan 19 13:42:17 2018 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2009-2017 Openbravo SLU + * All portions are Copyright (C) 2009-2018 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -31,6 +31,8 @@ * */ public class SessionInfo { + private static final String JDBC_CONNECTION_POOL_CLASS_NAME = "org.openbravo.apachejdbcconnectionpool.JdbcExternalConnectionPool"; + private static final Logger log4j = Logger.getLogger(SessionInfo.class); public static final String IMPORT_ENTRY_PROCESS = "IE"; @@ -40,6 +42,7 @@ */ private static boolean isAuditActive = false; private static boolean usageAuditActive = false; + private static boolean initialized = false; /* * The following variables track per thread the information about the current 'user' of the thread @@ -110,38 +113,80 @@ * Database, only action is take for POSTGRESQL */ public static void initDB(Connection conn, String rdbms) { + if (adContextInfoShouldBeCreated(conn, rdbms)) { + createAdContextInfoTable(conn); + } + } - if (rdbms != null && rdbms.equals("POSTGRE")) { - // Create temporary table - PreparedStatement psQuery = null; - PreparedStatement psCreate = null; - try { - if (conn.isReadOnly()) { - return; - } + private static void createAdContextInfoTable(Connection conn) { + // Create temporary table + PreparedStatement psCreate = null; + try { + StringBuffer sql = new StringBuffer(); + sql.append("CREATE TEMPORARY TABLE AD_CONTEXT_INFO"); + sql.append("(AD_USER_ID VARCHAR(32), "); + sql.append(" AD_SESSION_ID VARCHAR(32),"); + sql.append(" PROCESSTYPE VARCHAR(60), "); + sql.append(" PROCESSID VARCHAR(32)) on commit preserve rows"); + psCreate = getPreparedStatement(conn, sql.toString()); + psCreate.execute(); + } catch (Exception e) { + log4j.error("Error initializating audit infrastructure", e); + } finally { + releasePreparedStatement(psCreate); + } + } - psQuery = getPreparedStatement( - conn, - "select count(*) from information_schema.tables where table_name='ad_context_info' and table_type = 'LOCAL TEMPORARY'"); - ResultSet rs = psQuery.executeQuery(); + private static boolean adContextInfoShouldBeCreated(Connection conn, String rdbms) { + if (!isAuditActive || !isPosgreSQL(rdbms) || isReadOnly(conn)) { + return false; + } + if (usingJdbcConnectionPool()) { + // if this pool is used , the initDB method will only be called when creating a new + // connection, there is need to check if the ad_context_info has already been created + return true; + } else { + return !adContextInfoExists(conn); + } + } - if (rs.next() && rs.getString(1).equals("0")) { - StringBuffer sql = new StringBuffer(); - sql.append("CREATE TEMPORARY TABLE AD_CONTEXT_INFO"); - sql.append("(AD_USER_ID VARCHAR(32), "); - sql.append(" AD_SESSION_ID VARCHAR(32),"); - sql.append(" PROCESSTYPE VARCHAR(60), "); - sql.append(" PROCESSID VARCHAR(32)) on commit preserve rows"); - psCreate = getPreparedStatement(conn, sql.toString()); - psCreate.execute(); - } - } catch (Exception e) { - log4j.error("Error initializating audit infrastructure", e); - } finally { - releasePreparedStatement(psQuery); - releasePreparedStatement(psCreate); - } + private static boolean isReadOnly(Connection conn) { + boolean readOnly = false; + try { + readOnly = conn.isReadOnly(); + } catch (SQLException e) { + log4j.error("Error checking if the connection is read only", e); } + return readOnly; + } + + private static boolean isPosgreSQL(String rdbms) { + return rdbms != null && rdbms.equals("POSTGRE"); + } + + private static boolean usingJdbcConnectionPool() { + if (ExternalConnectionPool.getInstance() == null) { + return false; + } + return JDBC_CONNECTION_POOL_CLASS_NAME.equals(ExternalConnectionPool.getInstance().getClass() + .getName()); + } + + private static boolean adContextInfoExists(Connection conn) { + PreparedStatement psQuery = null; + boolean alreadyExists = false; + try { + psQuery = getPreparedStatement( + conn, + "select count(*) from information_schema.tables where table_name='ad_context_info' and table_type = 'LOCAL TEMPORARY'"); + ResultSet rs = psQuery.executeQuery(); + alreadyExists = rs.next() && !rs.getString(1).equals("0"); + } catch (SQLException e) { + log4j.error("Error checking if the ad_context_info table exists", e); + } finally { + releasePreparedStatement(psQuery); + } + return alreadyExists; } /** @@ -197,7 +242,7 @@ // autocommit true. boolean infoModified = Boolean.TRUE.equals(changedInfo.get()) || sessionConnection.get() == null || !conn.equals(sessionConnection.get()); - if (!infoModified || Boolean.FALSE.equals(auditThisThread.get()) || conn.isReadOnly()) { + if (!infoModified || Boolean.FALSE.equals(auditThisThread.get()) || isReadOnly(conn)) { return; } @@ -373,6 +418,11 @@ public static void setAuditActive(boolean isAuditActive) { SessionInfo.isAuditActive = isAuditActive; + SessionInfo.initialized = true; + } + + public static boolean isInitialized() { + return initialized; } static void setSessionConnection(Connection conn) { diff -r 28f74a227054 -r b42f32f0e0e9 src-db/database/sourcedata/AD_MENU.xml --- a/src-db/database/sourcedata/AD_MENU.xml Thu Feb 01 11:02:05 2018 +0000 +++ b/src-db/database/sourcedata/AD_MENU.xml Fri Jan 19 13:42:17 2018 +0100 @@ -4720,20 +4720,6 @@ <!--22FA719BCA6D47BE9F226057718308FA--> <OPENLINKINBROWSER><![CDATA[N]]></OPENLINKINBROWSER> <!--22FA719BCA6D47BE9F226057718308FA--></AD_MENU> -<!--2345D54C6D044A999C7FDD5C504008A6--><AD_MENU> -<!--2345D54C6D044A999C7FDD5C504008A6--> <AD_MENU_ID><![CDATA[2345D54C6D044A999C7FDD5C504008A6]]></AD_MENU_ID> -<!--2345D54C6D044A999C7FDD5C504008A6--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID> -<!--2345D54C6D044A999C7FDD5C504008A6--> <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID> -<!--2345D54C6D044A999C7FDD5C504008A6--> <ISACTIVE><![CDATA[Y]]></ISACTIVE> -<!--2345D54C6D044A999C7FDD5C504008A6--> <NAME><![CDATA[Update Audit Trail Infrastructure]]></NAME> -<!--2345D54C6D044A999C7FDD5C504008A6--> <DESCRIPTION><![CDATA[Update Audit Trail Infrastructure]]></DESCRIPTION> -<!--2345D54C6D044A999C7FDD5C504008A6--> <ISSUMMARY><![CDATA[N]]></ISSUMMARY> -<!--2345D54C6D044A999C7FDD5C504008A6--> <ACTION><![CDATA[P]]></ACTION> -<!--2345D54C6D044A999C7FDD5C504008A6--> <AD_PROCESS_ID><![CDATA[58763832F5F3485CAD33B8B9FCD6C640]]></AD_PROCESS_ID> -<!--2345D54C6D044A999C7FDD5C504008A6--> <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID> -<!--2345D54C6D044A999C7FDD5C504008A6--> <OPENLINKINBROWSER><![CDATA[N]]></OPENLINKINBROWSER> -<!--2345D54C6D044A999C7FDD5C504008A6--></AD_MENU> - <!--241C75FEC4C243BC8E71A4F358157F2D--><AD_MENU> <!--241C75FEC4C243BC8E71A4F358157F2D--> <AD_MENU_ID><![CDATA[241C75FEC4C243BC8E71A4F358157F2D]]></AD_MENU_ID> <!--241C75FEC4C243BC8E71A4F358157F2D--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID> diff -r 28f74a227054 -r b42f32f0e0e9 src-db/database/sourcedata/AD_MESSAGE.xml --- a/src-db/database/sourcedata/AD_MESSAGE.xml Thu Feb 01 11:02:05 2018 +0000 +++ b/src-db/database/sourcedata/AD_MESSAGE.xml Fri Jan 19 13:42:17 2018 +0100 @@ -20763,7 +20763,7 @@ <!--6392EEEB7C6F433BB2465DECD8BAAF35--> <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID> <!--6392EEEB7C6F433BB2465DECD8BAAF35--> <ISACTIVE><![CDATA[Y]]></ISACTIVE> <!--6392EEEB7C6F433BB2465DECD8BAAF35--> <VALUE><![CDATA[RegenerateAudit]]></VALUE> -<!--6392EEEB7C6F433BB2465DECD8BAAF35--> <MSGTEXT><![CDATA[In order to activate this change it is necessary to run the "Update Audit Trail Infrastructure" process.]]></MSGTEXT> +<!--6392EEEB7C6F433BB2465DECD8BAAF35--> <MSGTEXT><![CDATA[In order to activate this change it is necessary to build Openbravo and to restart Tomcat.]]></MSGTEXT> <!--6392EEEB7C6F433BB2465DECD8BAAF35--> <MSGTYPE><![CDATA[I]]></MSGTYPE> <!--6392EEEB7C6F433BB2465DECD8BAAF35--> <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID> <!--6392EEEB7C6F433BB2465DECD8BAAF35--> <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N> @@ -23100,7 +23100,7 @@ <!--926CA161FD5F49E5A993BED838A423D6--> <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID> <!--926CA161FD5F49E5A993BED838A423D6--> <ISACTIVE><![CDATA[Y]]></ISACTIVE> <!--926CA161FD5F49E5A993BED838A423D6--> <VALUE><![CDATA[RegenerateAudit_ExcludeColumn]]></VALUE> -<!--926CA161FD5F49E5A993BED838A423D6--> <MSGTEXT><![CDATA[In order to activate this change it is necessary to run the "Update Audit Trail Infrastructure" process.Use "Exclude Audit" in column to exclude some columns from Audit.]]></MSGTEXT> +<!--926CA161FD5F49E5A993BED838A423D6--> <MSGTEXT><![CDATA[In order to activate this change it is necessary to build Openbravo and to restart Tomcat..Use "Exclude Audit" in column to exclude some columns from Audit.]]></MSGTEXT> <!--926CA161FD5F49E5A993BED838A423D6--> <MSGTYPE><![CDATA[I]]></MSGTYPE> <!--926CA161FD5F49E5A993BED838A423D6--> <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID> <!--926CA161FD5F49E5A993BED838A423D6--> <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N> diff -r 28f74a227054 -r b42f32f0e0e9 src-db/database/sourcedata/AD_MODEL_OBJECT.xml --- a/src-db/database/sourcedata/AD_MODEL_OBJECT.xml Thu Feb 01 11:02:05 2018 +0000 +++ b/src-db/database/sourcedata/AD_MODEL_OBJECT.xml Fri Jan 19 13:42:17 2018 +0100 @@ -3359,18 +3359,6 @@ <!--405CB5D0FE124E4BBB9A6DC13A9F28EA--> <OBJECT_TYPE><![CDATA[S]]></OBJECT_TYPE> <!--405CB5D0FE124E4BBB9A6DC13A9F28EA--></AD_MODEL_OBJECT> -<!--40C9C50E51CF446C83AF1F4562D93FD3--><AD_MODEL_OBJECT> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <AD_MODEL_OBJECT_ID><![CDATA[40C9C50E51CF446C83AF1F4562D93FD3]]></AD_MODEL_OBJECT_ID> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <ISACTIVE><![CDATA[Y]]></ISACTIVE> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <ACTION><![CDATA[P]]></ACTION> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <CLASSNAME><![CDATA[org.openbravo.erpCommon.ad_process.UpdateAuditTrail]]></CLASSNAME> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <ISDEFAULT><![CDATA[Y]]></ISDEFAULT> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <AD_PROCESS_ID><![CDATA[58763832F5F3485CAD33B8B9FCD6C640]]></AD_PROCESS_ID> -<!--40C9C50E51CF446C83AF1F4562D93FD3--> <OBJECT_TYPE><![CDATA[P]]></OBJECT_TYPE> -<!--40C9C50E51CF446C83AF1F4562D93FD3--></AD_MODEL_OBJECT> - <!--411E148DD9524C6784F353977B2FC6E4--><AD_MODEL_OBJECT> <!--411E148DD9524C6784F353977B2FC6E4--> <AD_MODEL_OBJECT_ID><![CDATA[411E148DD9524C6784F353977B2FC6E4]]></AD_MODEL_OBJECT_ID> <!--411E148DD9524C6784F353977B2FC6E4--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID> diff -r 28f74a227054 -r b42f32f0e0e9 src-db/database/sourcedata/AD_PROCESS.xml --- a/src-db/database/sourcedata/AD_PROCESS.xml Thu Feb 01 11:02:05 2018 +0000 +++ b/src-db/database/sourcedata/AD_PROCESS.xml Fri Jan 19 13:42:17 2018 +0100 @@ -6294,33 +6294,6 @@ <!--58591E3E0F7648E4A09058E037CE49FC--> <ISKILLABLE><![CDATA[N]]></ISKILLABLE> <!--58591E3E0F7648E4A09058E037CE49FC--></AD_PROCESS> -<!--58763832F5F3485CAD33B8B9FCD6C640--><AD_PROCESS> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <AD_PROCESS_ID><![CDATA[58763832F5F3485CAD33B8B9FCD6C640]]></AD_PROCESS_ID> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <ISACTIVE><![CDATA[Y]]></ISACTIVE> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <VALUE><![CDATA[UpdateAuditTrail]]></VALUE> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <NAME><![CDATA[Update Audit Trail Infrastructure]]></NAME> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <DESCRIPTION><![CDATA[Update Audit Trail Infrastructure]]></DESCRIPTION> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <HELP><![CDATA[This process updates the infrastructure for a complete audit of the selected tables.<br/> -This process:<br/> -- Will Stop the background process scheduler during process execution<br/> -- Can take several minutes. Do not close the browser during execution.]]></HELP> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <ACCESSLEVEL><![CDATA[4]]></ACCESSLEVEL> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <ISUSERSTARTABLE><![CDATA[N]]></ISUSERSTARTABLE> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <ISREPORT><![CDATA[N]]></ISREPORT> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <ISDIRECTPRINT><![CDATA[N]]></ISDIRECTPRINT> -<!--58763832F5F3485CAD33B8B9FCD6C640--> <ISBACKGROUND><![CDATA[N]]></ISBACKGROUND> ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits