From c328aa11551418cd5e31d71d60f7897d2cf47ae2 Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Tue, 11 Feb 2014 20:19:27 +0000 Subject: [PATCH] Merged HEAD-BUG-FIX (4.3/Cloud) to HEAD (4.3/Cloud) 57231: Merged V4.2-BUG-FIX (4.2.1) to HEAD-BUG-FIX (Cloud/4.3) 57114: Merged V4.1-BUG-FIX (4.1.7) to V4.2-BUG-FIX (4.2.1) 56975: MNT-9821 Merged V4.1.3 (4.1.3.16) to V4.1-BUG-FIX (4.1.7) 56834: Merged DEV to PATCHES/V4.1.3 56783: (RECORD ONLY) Branch for MNT-9740 diagnostic changes and fixes 56784: Diagnostic patch to Hibernate's BorrowedConnectionProxy to facilitate leak detection (MNT-9740) - This feature will activate on first hit. If that is too late, turn on debug: log4j.logger.org.hibernate.jdbc.BorrowedConnectionProxy=DEBUG git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@61786 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../BorrowedConnectionProxyTest.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 source/java/org/alfresco/repo/domain/hibernate/BorrowedConnectionProxyTest.java diff --git a/source/java/org/alfresco/repo/domain/hibernate/BorrowedConnectionProxyTest.java b/source/java/org/alfresco/repo/domain/hibernate/BorrowedConnectionProxyTest.java new file mode 100644 index 0000000000..3ad500b41b --- /dev/null +++ b/source/java/org/alfresco/repo/domain/hibernate/BorrowedConnectionProxyTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.domain.hibernate; + +import java.sql.Connection; + +import javax.sql.DataSource; +import javax.transaction.UserTransaction; + +import junit.framework.TestCase; + +import org.alfresco.service.transaction.TransactionService; +import org.alfresco.util.ApplicationContextHelper; +import org.hibernate.HibernateException; +import org.hibernate.jdbc.BorrowedConnectionProxy; +import org.springframework.context.ApplicationContext; +import org.springframework.jdbc.datasource.ConnectionHolder; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** + * Tests the {@link BorrowedConnectionProxy}'s ability to detect post-close reuse. + * + * @author Derek Hulley + */ +public class BorrowedConnectionProxyTest extends TestCase +{ + private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); + + private TransactionService transactionService; + private DataSource dataSource; + + public void setUp() throws Exception + { + transactionService = (TransactionService) ctx.getBean("transactionComponent"); + dataSource = (DataSource) ctx.getBean("dataSource"); + } + + public void testSimpleCommit() throws Throwable + { + UserTransaction txn = transactionService.getUserTransaction(); + try + { + txn.begin(); + txn.commit(); + } + catch (Throwable e) + { + try { txn.rollback(); } catch (Throwable ee) {} + throw e; + } + } + + public void testLoggingAutoActivate() throws Throwable + { + assertFalse("Auto-logging of misuse of the wrapper should be off", BorrowedConnectionProxy.isCallStackTraced()); + + UserTransaction txn = transactionService.getUserTransaction(); + Connection connection; + try + { + txn.begin(); + // Dig the proxy out of ... somewhere + ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); + connection = conHolder.getConnection(); + txn.commit(); + } + catch (Throwable e) + { + try { txn.rollback(); } catch (Throwable ee) {} + throw e; + } + // Now mess with the connection, which is protected by the Hibernate wrapper + try + { + connection.commit(); + fail("Use case should have generated a HibernateException"); + } + catch (HibernateException e) + { + // Expected + } + assertTrue("Auto-logging of misuse of the wrapper should now be on", BorrowedConnectionProxy.isCallStackTraced()); + + // Now start a new transaction and we should see logging + txn = transactionService.getUserTransaction(); + try + { + txn.begin(); + // Dig the proxy out of ... somewhere + ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); + connection = conHolder.getConnection(); + txn.commit(); + } + catch (Throwable e) + { + try { txn.rollback(); } catch (Throwable ee) {} + throw e; + } + // Now mess with the connection, which is protected by the Hibernate wrapper + try + { + connection.commit(); + fail("Use case should have generated a HibernateException"); + } + catch (HibernateException e) + { + // Expected + } + // Check for error logs + } +}