001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.webapp.servlets;
006:
007: import org.hibernate.LockMode;
008: import org.hibernate.Session;
009: import org.hibernate.stat.QueryStatistics;
010: import org.hibernate.stat.Statistics;
011:
012: import com.tc.util.Assert;
013: import com.tctest.domain.Account;
014: import com.tctest.domain.Event;
015: import com.tctest.domain.EventManager;
016: import com.tctest.domain.HibernateUtil;
017: import com.tctest.domain.Person;
018: import com.tctest.domain.PhoneNumber;
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.util.Date;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Set;
027:
028: import javax.servlet.http.HttpServlet;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.http.HttpSession;
032:
033: public final class ContainerHibernateTestServlet extends HttpServlet {
034:
035: public void doGet(HttpServletRequest request,
036: HttpServletResponse response) throws IOException {
037: HttpSession session = request.getSession(true);
038: response.setContentType("text/plain");
039: PrintWriter out = response.getWriter();
040:
041: String server = request.getParameter("server");
042: if ("server0".equals(server)) {
043: try {
044: doServer0(session);
045: out.println("OK");
046: } catch (Exception e) {
047: e.printStackTrace(out);
048: }
049: } else if ("server1".equals(server)) {
050: try {
051: doServer1(session);
052: out.println("OK");
053: } catch (Exception e) {
054: e.printStackTrace(out);
055: }
056: }
057: out.flush();
058: }
059:
060: private void doServer0(HttpSession httpSession) throws Exception {
061: HibernateUtil.dropAndCreateDatabaseSchema();
062: EventManager mgr = new EventManager();
063:
064: // create 3 persons Steve, Orion, Tim
065: Long steveId = mgr.createAndStorePerson("Steve", "Harris");
066: mgr.addEmailToPerson(steveId, "steve@tc.com");
067: mgr.addEmailToPerson(steveId, "sharrif@tc.com");
068:
069: PhoneNumber p1 = new PhoneNumber();
070: p1.setNumberType("Office");
071: p1.setPhone(111111);
072: mgr.addPhoneNumberToPerson(steveId, p1);
073:
074: PhoneNumber p2 = new PhoneNumber();
075: p2.setNumberType("Home");
076: p2.setPhone(222222);
077: mgr.addPhoneNumberToPerson(steveId, p2);
078:
079: Long orionId = mgr.createAndStorePerson("Orion", "Letizi");
080: mgr.addEmailToPerson(orionId, "orion@tc.com");
081:
082: Long timId = mgr.createAndStorePerson("Tim", "Teck");
083: mgr.addEmailToPerson(timId, "teck@tc.com");
084:
085: Long engMeetingId = mgr.createAndStoreEvent("Eng Meeting",
086: new Date());
087: mgr.addPersonToEvent(steveId, engMeetingId);
088: mgr.addPersonToEvent(orionId, engMeetingId);
089: mgr.addPersonToEvent(timId, engMeetingId);
090:
091: Long docMeetingId = mgr.createAndStoreEvent("Doc Meeting",
092: new Date());
093: mgr.addPersonToEvent(steveId, docMeetingId);
094: mgr.addPersonToEvent(orionId, docMeetingId);
095:
096: List eventList = mgr.listEvents();
097:
098: // list emails of people in Eng meeting
099: List emailList = mgr.listEmailsOfEvent(engMeetingId);
100: System.out.println("Email list for Eng meeting: " + emailList);
101: httpSession.setAttribute("events", eventList);
102: httpSession.setAttribute("event", eventList.get(0));
103: System.out.println("added event list to session");
104:
105: Account acc = new Account();
106: Long accId = mgr.addPersonToAccount(steveId, acc);
107: acc = mgr.getAccount(accId);
108: httpSession.setAttribute("acc", acc);
109:
110: HibernateUtil.getSessionFactory().close();
111: }
112:
113: private void doServer1(HttpSession httpSession) throws Exception {
114: Session sessionTmp = HibernateUtil.getSessionFactory()
115: .getCurrentSession();
116: sessionTmp.beginTransaction();
117: Account acc = (Account) httpSession.getAttribute("acc");
118: sessionTmp.lock(acc, LockMode.NONE);
119: System.err.println("acc.person: " + acc.getPerson());
120: sessionTmp.getTransaction().commit();
121:
122: EventManager mgr = new EventManager();
123: Statistics stats = HibernateUtil.getSessionFactory()
124: .getStatistics();
125: stats.setStatisticsEnabled(true);
126:
127: // this will get the data from ehcache
128: List events = mgr.listEvents();
129:
130: Session session = HibernateUtil.getSessionFactory()
131: .getCurrentSession();
132: session.beginTransaction();
133:
134: // reassociate transient pojos to this session
135: for (Iterator it = events.iterator(); it.hasNext();) {
136: session.lock((Event) it.next(), LockMode.NONE);
137: }
138:
139: System.out.println("events: " + events);
140: Assert.assertTrue(events.size() >= 2);
141:
142: // list people in first event
143: Event event = (Event) events.get(0);
144: Set people = event.getParticipants();
145: System.out.println("people: " + people);
146:
147: // list emails of people from first event
148: Set emails = new HashSet();
149: for (Iterator it = people.iterator(); it.hasNext();) {
150: Person person = (Person) it.next();
151: emails.addAll(person.getEmailAddresses());
152: }
153: System.out.println("emails: " + emails);
154: Assert.assertTrue(emails.size() >= 3);
155:
156: Set phones = new HashSet();
157: for (Iterator it = people.iterator(); it.hasNext();) {
158: Person person = (Person) it.next();
159: phones.addAll(person.getPhoneNumbers());
160: }
161: System.out.println("phones: " + phones);
162: Assert.assertTrue(phones.size() >= 2);
163:
164: session.getTransaction().commit();
165: HibernateUtil.getSessionFactory().close();
166: System.out.println("DONE!");
167:
168: QueryStatistics queryStats = stats
169: .getQueryStatistics("from Event");
170: System.out.println("Event query cache hit: "
171: + queryStats.getCacheHitCount());
172:
173: Assert.assertEquals(1, queryStats.getCacheHitCount());
174: }
175: }
|