Source Code Cross Referenced for OptimisticLockingMultithreadedTest.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » 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 » Database ORM » db ojb » org.apache.ojb.broker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.broker;
002:
003:        import org.apache.commons.lang.SerializationUtils;
004:        import org.apache.ojb.broker.util.logging.LoggerFactory;
005:        import org.apache.ojb.junit.JUnitExtensions;
006:
007:        /**
008:         * Test optimistic-locking implementation with multiple threads.
009:         * Different threads try to update the same instance / or a copy
010:         * of the same object.
011:         *
012:         * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
013:         * @version $Id: OptimisticLockingMultithreadedTest.java,v 1.1.2.2 2004/12/12 01:35:12 arminw Exp $
014:         */
015:        public class OptimisticLockingMultithreadedTest extends
016:                JUnitExtensions.MultiThreadedTestCase {
017:            private static int threadCount;
018:            static final String msg = "Thread write order: ";
019:
020:            public static void main(String[] args) {
021:                String[] arr = { OptimisticLockingMultithreadedTest.class
022:                        .getName() };
023:                junit.textui.TestRunner.main(arr);
024:            }
025:
026:            public OptimisticLockingMultithreadedTest(String s) {
027:                super (s);
028:            }
029:
030:            protected void setUp() throws Exception {
031:                super .setUp();
032:            }
033:
034:            protected void tearDown() throws Exception {
035:                super .tearDown();
036:            }
037:
038:            public void testLockingOfObject() throws Exception {
039:                LockedByVersion targetObject = createLockedByVersion();
040:                storeObject(targetObject);
041:
042:                // number of concurrent threads to run
043:                int threads = 6;
044:                // number of updates each thread performs against the object
045:                int objectUpdates = 20;
046:
047:                TestCaseRunnable tct[] = new TestCaseRunnable[threads];
048:                for (int i = 0; i < threads; i++) {
049:                    /*
050:                    several threads try to lock the same object (shared object),
051:                    the other threads lock deep copies of the shared object
052:                     */
053:                    if (i % 2 == 0)
054:                        tct[i] = new LockHandle(targetObject, objectUpdates);
055:                    else
056:                        tct[i] = new LockHandle(
057:                                (LockedByVersion) SerializationUtils
058:                                        .clone(targetObject), objectUpdates);
059:                }
060:                System.out.println("*** START - Multithreaded lock test ***");
061:                System.out.println("Number of concurrent threads: " + threads);
062:                System.out.println("Number of object updates per thread: "
063:                        + objectUpdates);
064:                System.out
065:                        .println("Each thread try to update the same object. If an OptimisticLockException"
066:                                + " was thrown, the thread wait and try later again (200 attempts, then fail)");
067:                // run test classes
068:                runTestCaseRunnables(tct);
069:                System.out.println(targetObject.getValue());
070:                System.out
071:                        .println("An '-' indicate write success at first attempt");
072:                System.out
073:                        .println("An '+' indicate write success after several OptimisticLockException");
074:                System.out.println("*** END - Multithreaded lock test  ***");
075:            }
076:
077:            private LockedByVersion createLockedByVersion() {
078:                int number = newThreadKey();
079:                LockedByVersion lo = new LockedByVersion();
080:                lo.setValue(msg + number);
081:                return lo;
082:            }
083:
084:            private void storeObject(Object obj) throws Exception {
085:                PersistenceBroker broker = PersistenceBrokerFactory
086:                        .defaultPersistenceBroker();
087:                try {
088:                    broker.beginTransaction();
089:                    broker.store(obj);
090:                    broker.commitTransaction();
091:                } finally {
092:                    if (broker != null)
093:                        broker.close();
094:                }
095:            }
096:
097:            public static synchronized int newThreadKey() {
098:                return threadCount++;
099:            }
100:
101:            //=======================================================================
102:            // inner classes
103:            //=======================================================================
104:
105:            class LockHandle extends TestCaseRunnable {
106:                LockedByVersion obj;
107:                int threadNumber;
108:                int objectUpdates = 30;
109:
110:                public LockHandle(LockedByVersion obj, int objectUpdates) {
111:                    super ();
112:                    this .objectUpdates = objectUpdates;
113:                    this .obj = obj;
114:                }
115:
116:                public void runTestCase() throws Throwable {
117:                    threadNumber = newThreadKey();
118:                    for (int i = 0; i < objectUpdates; i++) {
119:                        updateObject(obj, false);
120:                    }
121:                }
122:
123:                private int counter = 0;
124:                private static final int maxAttempts = 200;
125:                private static final int nearMax = (int) (maxAttempts * 0.9);
126:
127:                private void updateObject(LockedByVersion obj,
128:                        boolean LNGEthrown) throws Exception {
129:                    PersistenceBroker broker = PersistenceBrokerFactory
130:                            .defaultPersistenceBroker();
131:                    try {
132:                        broker.beginTransaction();
133:                        updateName(obj, LNGEthrown);
134:                        broker.store(obj);
135:                        broker.commitTransaction();
136:                    } catch (OptimisticLockException e) {
137:                        if (broker != null) {
138:                            broker.abortTransaction();
139:                            broker.close();
140:                        }
141:                        // we try X times again to update the object
142:                        if (counter < maxAttempts) {
143:                            counter++;
144:                            if (counter > nearMax)
145:                                LoggerFactory
146:                                        .getDefaultLogger()
147:                                        .warn(
148:                                                "OptimisticLockingMultithreadedTest: thread "
149:                                                        + threadNumber
150:                                                        + " waits "
151:                                                        + counter
152:                                                        + " times to update object. Maximal attempts before fail are "
153:                                                        + maxAttempts
154:                                                        + ". This can be a result of low hardware.");
155:                            Thread.sleep(10);
156:                            PersistenceBroker pb = PersistenceBrokerFactory
157:                                    .defaultPersistenceBroker();
158:                            LockedByVersion temp;
159:                            try {
160:                                // lookup object instance again to get vaild lock value
161:                                Identity oid = pb.serviceIdentity()
162:                                        .buildIdentity(obj);
163:                                temp = (LockedByVersion) pb
164:                                        .getObjectByIdentity(oid);
165:                            } finally {
166:                                if (pb != null)
167:                                    pb.close();
168:                            }
169:                            updateObject(temp, true);
170:                        } else {
171:                            LoggerFactory.getDefaultLogger().error(
172:                                    "* Can't lock given object, will throw exception"
173:                                            + " for thread number "
174:                                            + threadNumber + " *");
175:                            throw e;
176:                        }
177:                    } finally {
178:                        counter = 0;
179:                        if (broker != null) {
180:                            broker.close();
181:                        }
182:                    }
183:                }
184:
185:                private void updateName(LockedByVersion obj, boolean LNGEthrown) {
186:                    String token;
187:                    if (LNGEthrown) {
188:                        token = "+";
189:                    } else {
190:                        token = "-";
191:                    }
192:                    if (obj.getValue().length() < 120) {
193:                        obj.setValue(obj.getValue() + token + threadNumber);
194:                    } else {
195:                        System.out.println(obj.getValue());
196:                        obj.setValue(msg + threadNumber);
197:                    }
198:                }
199:            }
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.