Source Code Cross Referenced for TxConnectionsThreadLocal.java in  » J2EE » jfox » org » jfox » ejb3 » transaction » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » jfox » org.jfox.ejb3.transaction 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JFox - The most lightweight Java EE Application Server!
003:         * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004:         *
005:         * JFox is licenced and re-distributable under GNU LGPL.
006:         */
007:        package org.jfox.ejb3.transaction;
008:
009:        import java.sql.Connection;
010:        import java.util.ArrayList;
011:        import java.util.List;
012:        import java.util.Iterator;
013:        import javax.transaction.Transaction;
014:        import javax.transaction.Status;
015:        import javax.transaction.SystemException;
016:
017:        import org.apache.log4j.Logger;
018:
019:        /**
020:         * 通过 ThreadLocal 关� Transaction,以� Transaction 使用的 Connection,
021:         * 以便在 Transaction 结�的时候,释放connection,使得 connection �以被 pool 回收
022:         *
023:         * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
024:         */
025:        public class TxConnectionsThreadLocal {
026:
027:            private static Logger logger = Logger
028:                    .getLogger(TxConnectionsThreadLocal.class);
029:
030:            /**
031:             * ThreadLocal ä¿?å­˜ Thread => TransactionConnectionPair
032:             */
033:            private static ThreadLocal<TxConnectionWrapper> Thread2TxConn = new ThreadLocal<TxConnectionWrapper>();
034:
035:            public static void setTransaction(Transaction transaction) {
036:                logger.debug("Attach transaction to thread, transaction is: "
037:                        + transaction);
038:
039:                TxConnectionWrapper txConnectionWrapper = Thread2TxConn.get();
040:                if (txConnectionWrapper == null) { // 该线程还未�起新的事务
041:                    txConnectionWrapper = new TxConnectionWrapper(transaction);
042:                    Thread2TxConn.set(txConnectionWrapper);
043:                } else { // 该线程已�有事务,但��起一个新的事务,新�起的事务接下�将是活动事务
044:                    txConnectionWrapper.addTransaction(transaction);
045:                }
046:            }
047:
048:            public static void addConnection2Tx(Connection connection) {
049:                logger
050:                        .debug("Attatch connection to transaction, connection is: "
051:                                + connection);
052:                TxConnectionWrapper txConnectionWrapper = Thread2TxConn.get();
053:
054:                if (txConnectionWrapper == null) {
055:                    logger
056:                            .warn("Attatch connection to transaction failed, no transaction!");
057:                } else {
058:                    txConnectionWrapper.addConnection(connection);
059:                }
060:            }
061:
062:            public static void releaseTxConnections() {
063:                logger.debug("Release Transaction attatched connections.");
064:                TxConnectionWrapper txConnectionWrapper = Thread2TxConn.get();
065:                if (txConnectionWrapper == null) {
066:                    logger
067:                            .warn("Release attatched connections failed, current transaction is null.");
068:                } else {
069:                    txConnectionWrapper.releaseCurrentTxConnections();
070:                    // 如果没有剩余的事务环境,清除 ThreadLocal ��
071:                    if (txConnectionWrapper.isEmpty()) {
072:                        logger
073:                                .debug("Remove thread local transaction binding.");
074:                        Thread2TxConn.remove();
075:                    }
076:                }
077:            }
078:
079:            /**
080:             * �存和 Thread 关�的 Transaction 以� Connection
081:             */
082:            static class TxConnectionWrapper {
083:                // 线程关�的事务链,最�一个事务��活动事务
084:                List<Transaction> transactionChain = new ArrayList<Transaction>();
085:
086:                // 线程关�的 TransactionConnectionPair
087:                List<TransactionConnectionPair> txconns = new ArrayList<TransactionConnectionPair>();
088:
089:                public TxConnectionWrapper(Transaction transaction) {
090:                    transactionChain.add(transaction);
091:                }
092:
093:                public void addTransaction(Transaction transaction) {
094:                    transactionChain.add(transaction);
095:                }
096:
097:                public void addConnection(Connection connection) {
098:                    // 当�事务�为活动事务
099:                    txconns.add(new TransactionConnectionPair(transactionChain
100:                            .get(transactionChain.size() - 1), connection));
101:                }
102:
103:                /**
104:                 * 释放当�活动事务关�所有 connection
105:                 */
106:                public void releaseCurrentTxConnections() {
107:                    // 根�First Created, last commit 原则,活动事务为最�一个事务
108:                    Transaction activeTransaction = transactionChain
109:                            .remove(transactionChain.size() - 1);
110:
111:                    try {
112:                        if (activeTransaction.getStatus() != Status.STATUS_COMMITTED
113:                                && activeTransaction.getStatus() != Status.STATUS_ROLLEDBACK) {
114:                            logger
115:                                    .warn("Release connections attached non ended transaction!");
116:                        }
117:                    } catch (SystemException e) {
118:                        logger.error("Get transaction status exception.", e);
119:                    }
120:
121:                    Iterator<TransactionConnectionPair> it = txconns.iterator();
122:                    while (it.hasNext()) {
123:                        TransactionConnectionPair tcp = it.next();
124:                        if (tcp.transaction == activeTransaction) {
125:                            try {
126:                                it.remove();
127:                                tcp.connection.close();
128:                            } catch (Throwable e) {
129:                                logger
130:                                        .warn(
131:                                                "Connection close in release current transaction atteched connection with exception.",
132:                                                e);
133:                            }
134:                        }
135:                    }
136:                }
137:
138:                public boolean isEmpty() {
139:                    return transactionChain.isEmpty();
140:                }
141:
142:                protected void finalize() throws Throwable {
143:                    super .finalize();
144:                    if (!txconns.isEmpty()) {
145:                        for (TransactionConnectionPair tcp : txconns) {
146:                            try {
147:                                tcp.connection.close();
148:                            } catch (Throwable e) {
149:                                logger
150:                                        .warn(
151:                                                " Connection close in finalize with exception.",
152:                                                e);
153:                            }
154:                        }
155:                    }
156:                }
157:            }
158:
159:            static class TransactionConnectionPair {
160:                Transaction transaction;
161:                Connection connection;
162:
163:                public TransactionConnectionPair(Transaction transaction,
164:                        Connection connection) {
165:                    this .transaction = transaction;
166:                    this .connection = connection;
167:                }
168:            }
169:
170:            public static void main(String[] args) {
171:
172:            }
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.