View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * Created on 2005/07/31
18   */
19  package org.seasar.tuigwaa.database;
20  
21  import java.sql.Connection;
22  import java.sql.SQLException;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.transaction.Synchronization;
28  import javax.transaction.Transaction;
29  import javax.transaction.TransactionManager;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.hibernate.FlushMode;
34  import org.hibernate.Session;
35  import org.hibernate.SessionFactory;
36  import org.seasar.extension.jdbc.util.ConnectionUtil;
37  import org.seasar.framework.exception.SQLRuntimeException;
38  import org.seasar.framework.util.TransactionManagerUtil;
39  import org.seasar.framework.util.TransactionUtil;
40  
41  /***
42   * @author nishioka
43   */
44  public class DynaSessionFactoryImpl implements DynaSessionFactory,
45  		Synchronization {
46  
47  	private Log log = LogFactory.getLog(getClass());
48  
49  	private final Map configManager = Collections
50  			.synchronizedMap(new HashMap());
51  
52  	private final TransactionManager transactionManager;
53  
54  	private final BasicDatabaseService dbService;
55  
56  	private final Map txSessions = Collections.synchronizedMap(new HashMap());
57  
58  	public DynaSessionFactoryImpl(TransactionManager transactionManager,
59  			BasicDatabaseService dbService) {
60  		this.transactionManager = transactionManager;
61  		this.dbService = dbService;
62  	}
63  
64  	public void deleteConfiguration(String domainName) {
65  		removeConfiguration(domainName);
66  	}
67  
68  	public TransactionManager getTransactionManager() {
69  		return transactionManager;
70  	}
71  
72  	public synchronized SessionFactory getSessionFactory() {
73  		return getSessionFactory(null);
74  	}
75  
76  	public synchronized SessionFactory getSessionFactory(String domainName) {
77  		DynaConfiguration cfg = getConfiguration(domainName);
78  		SessionFactory factory = null;
79  		factory = cfg.getSessionFactory();
80  		return factory;
81  	}
82  
83  	public DynaConfiguration getConfiguration(String domainName) {
84  		DynaConfiguration cfg = (DynaConfiguration) configManager.get(domainName);
85  		if (cfg == null) {
86  			String driver = dbService.getDriver(domainName);
87  			cfg = new DynaConfiguration(driver);
88  			putConfiguration(domainName, cfg);
89  		}
90  		return cfg;
91  	}
92  
93  	public int getTxSessionSize() {
94  		return txSessions.size();
95  	}
96  
97  	public Session getSession() {
98  		return getSession(null);
99  	}
100 
101 	public Session getSession(String domainName) {
102 		Transaction tx = getTransaction();
103 		if (tx == null) {
104 			return createSession(domainName);
105 		}
106 		Session session = (Session) txSessions.get(tx);
107 		if (session != null && session.isOpen()) {
108 			return session;
109 		}
110 		return bindSession(tx, domainName);
111 	}
112 
113 	public Connection getConnection(String domainName) {
114 		try {
115 			return dbService.getConnection(domainName);
116 		} catch (SQLException e) {	
117 			throw new SQLRuntimeException(e);
118 		}
119 	}
120 
121 	public void beforeCompletion() {
122 		flushSession();
123 	}
124 
125 	public void afterCompletion(int arg0) {
126 		closeSession();
127 	}
128 
129 	// [Start] ----- Private Method -----
130 
131 	private Session bindSession(Transaction tx, String schema) {
132 		Session session = (Session) txSessions.get(tx);
133 		if (session != null && session.isOpen()) {
134 			return session;
135 		}
136 		session = createSession(schema);
137 		txSessions.put(tx, session);
138 		TransactionUtil.registerSynchronization(tx, this);
139 		return session;
140 	}
141 
142 	private void closeSession() {
143 		Transaction tx = getTransaction();
144 		if (tx == null) {
145 			return;
146 		}
147 
148 		Session session = (Session) txSessions.remove(tx);
149 		if (session != null && session.isOpen()) {
150 			try {
151 				session.clear();
152 			} finally {
153 				Connection con = session.close();
154 				ConnectionUtil.close(con);
155 			}
156 		}
157 	}
158 
159 	private void flushSession() {
160 		Transaction tx = getTransaction();
161 		if (tx == null) {
162 			return;
163 		}
164 		Session session = (Session) txSessions.get(tx);
165 		if (session == null || !session.isOpen()
166 				|| session.getFlushMode().equals(FlushMode.NEVER)) {
167 			return;
168 		}
169 		session.flush();
170 	}
171 
172 	private void putConfiguration(String domainName, DynaConfiguration cfg) {
173 		configManager.put(domainName, cfg);
174 		log.info("Put Configuration :" + cfg);
175 	}
176 
177 	private void removeConfiguration(String domainName) {
178 		configManager.remove(domainName);
179 	}
180 
181 	private Transaction getTransaction() {
182 		return TransactionManagerUtil.getTransaction(getTransactionManager());
183 	}
184 
185 	private Session createSession(String domainName) {
186 		SessionFactory factory = getSessionFactory(domainName);
187 		return factory.openSession(getConnection(domainName));
188 	}
189 }