Source Code Cross Referenced for NestedTransactionWrapper.java in  » Web-Framework » RSF » uk » org » ponder » 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 » Web Framework » RSF » uk.org.ponder.transaction 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on Nov 10, 2004
003:         */
004:        package uk.org.ponder.transaction;
005:
006:        import uk.org.ponder.util.Logger;
007:        import uk.org.ponder.util.UniversalRuntimeException;
008:
009:        /**
010:         * @author Antranig Basman (antranig@caret.cam.ac.uk)
011:         *  
012:         */
013:        public class NestedTransactionWrapper implements  Transaction {
014:            private TransactionThreadMap transmap;
015:            private Transaction target;
016:            private Transaction listener;
017:
018:            /** Given factories for the main target and listener "flat" transactions
019:             * that will be wrapped by the required NestedTransactionFactory,
020:             * inspect the current threadmap and return any existing NestedTransaction
021:             * which is in progress with its use count updated, or create a new one
022:             * using the factories if requred and store it.
023:             * @param mainfactory A factory producing the main logic flat transaction
024:             * to be wrapped.
025:             * @param listenerfactory A factory producing a "listener" transaction
026:             * that requires (synchronous) notification AFTER successful conclusion 
027:             * of transation events. This parameter may be <code>null</code>
028:             */
029:            public static NestedTransactionWrapper beginNestedTransaction(
030:                    TransactionFactory mainfactory,
031:                    TransactionFactory listenerfactory,
032:                    TransactionThreadMap transmap) {
033:                NestedTransactionWrapper togo = (NestedTransactionWrapper) transmap
034:                        .getTransaction();
035:                if (togo == null) {
036:                    Transaction nested = mainfactory.beginTransaction();
037:                    togo = new NestedTransactionWrapper(nested,
038:                            listenerfactory == null ? null : listenerfactory
039:                                    .beginTransaction(), transmap);
040:                } else {
041:                    togo.increment();
042:                }
043:                return togo;
044:            }
045:
046:            /**
047:             * Accepts two Transactions objects, the first being the "important" concrete
048:             * transaction object returned by the application, the second being a
049:             * subsidiary listener. Both will be notified of transaction events, the
050:             * listener receiving notification second and with lower priority in the case
051:             * of exceptions.
052:             * 
053:             * @param target
054:             * @param listener
055:             */
056:            public NestedTransactionWrapper(Transaction target,
057:                    Transaction listener, TransactionThreadMap transmap) {
058:                this .target = target;
059:                this .listener = listener;
060:                this .transmap = transmap;
061:                transmap.enterTransaction(this );
062:            }
063:
064:            static final int ROLLED_BACK = -1;
065:
066:            int nestingdepth = 1;
067:
068:            public void increment() {
069:                ++nestingdepth;
070:            }
071:
072:            public void commit() {
073:                // A commit has no effect except at the very highest stack level attempting.
074:                // Two-phase commit is not supported, to the extent that any exception
075:                // occuring within a commit will inevitably cause all correct client
076:                // code to perform a rollback within a finally block. No point writing
077:                // any extra code here to deal with it.
078:                if (nestingdepth <= 0) {
079:                    throw new UniversalRuntimeException(
080:                            "Error: attempting to commit inactive transaction with count "
081:                                    + nestingdepth);
082:                }
083:                --nestingdepth;
084:                if (nestingdepth == 0) {
085:                    try {
086:                        target.commit();
087:                        transmap.endTransaction();
088:                        if (listener != null) {
089:                            listener.commit();
090:                        }
091:                    } catch (Throwable t) {
092:                        throw UniversalRuntimeException.accumulate(t,
093:                                "Error committing transaction");
094:                    }
095:                }
096:            }
097:
098:            public void rollback() {
099:                if (nestingdepth == 0) {
100:                    Logger.log
101:                            .fatal("Fatal logic error: Attempting to roll back transaction which has already been committed");
102:                    return;
103:                }
104:                // Having rolled back the transaction once, it is removed from the map
105:                // so that a fresh transaction may be started. However, further people
106:                // up the stack may attempt independently to roll the transaction back
107:                // again from their local copies, this will have no further effect.
108:                if (nestingdepth != ROLLED_BACK) {
109:                    try {
110:                        target.rollback();
111:                    } catch (Throwable t) {
112:                        // THIS METHOD SHOULD NEVER THROW!!!
113:                        Logger.log.fatal("Error rolling back transaction");
114:                    } finally {
115:                        nestingdepth = ROLLED_BACK;
116:                        transmap.endTransaction();
117:                        if (listener != null) {
118:                            listener.rollback();
119:                        }
120:                    }
121:                }
122:            }
123:
124:            public String toString() {
125:                return target.toString();
126:            }
127:
128:            public int hashCode() {
129:                return target.hashCode();
130:            }
131:
132:            // we can compare equal either to another nested wrapper mapping
133:            // the same target, or the same target itself.
134:            public boolean equals(Object other) {
135:                if (other instanceof  NestedTransactionWrapper) {
136:                    return target
137:                            .equals(((NestedTransactionWrapper) other).target);
138:                } else
139:                    return target.equals(other);
140:            }
141:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.