KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > impl > SQLPersistenceManagerFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * SQLPersistenceManagerFactory.java
26  *
27  * Created on March 6, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.impl;
31
32 import com.sun.jdo.api.persistence.support.*;
33 import com.sun.jdo.spi.persistence.utility.I18NHelper;
34 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceStore;
35 import com.sun.jdo.spi.persistence.support.sqlstore.SQLStoreManager;
36 import com.sun.jdo.spi.persistence.support.sqlstore.VersionConsistencyCache;
37 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper;
38 import com.sun.jdo.spi.persistence.utility.BucketizedHashtable;
39 import com.sun.jdo.spi.persistence.utility.logging.Logger;
40 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager;
41
42 //Remove this once checkLogger() is removed from initialize
43

44 import javax.sql.DataSource JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.io.PrintWriter JavaDoc;
47 import java.sql.Connection JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.Properties JavaDoc;
50 import java.util.ResourceBundle JavaDoc;
51
52 /**
53  *
54  * @author Marina Vatkina
55  * @version 0.1
56  */

57
58 public class SQLPersistenceManagerFactory
59         implements com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManagerFactory {
60
61     private PersistenceStore _store = null;
62     private ConnectionFactory _connectionFactory = null;
63     private Object JavaDoc _dataSource = null;
64     private PersistenceManagerFactory _persistenceManagerFactory = null;
65
66     /**
67      * PersistenceManager and Transaction default flags
68      */

69     private boolean optimistic = true;
70     private boolean retainValues = true;
71     private boolean nontransactionalRead = true;
72     private boolean ignoreCache = true;
73
74     /**
75      * sql Statement timeouts
76      */

77     private int queryTimeout = 0;
78     private int updateTimeout = 0;
79
80     /** Pooling size
81      */

82     private int minPool = 0;
83     private int maxPool = 0;
84
85     /**
86      * The logger
87      */

88     private static Logger logger = LogHelperPersistenceManager.getLogger();
89
90     /**
91      * I18N message handler
92      */

93     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
94             "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", //NOI18N
95
SQLPersistenceManagerFactory.class.getClassLoader());
96
97     /**
98      * bucket size for Transactional cache of PersistenceManager instances
99      */

100     private static int pmCacheBucketSize;
101
102     /**
103      * initial capacity for Transactional cache of PersistenceManager instances
104      */

105     private static int pmCacheInitialCapacity;
106
107     static {
108         pmCacheBucketSize = Integer.getInteger(
109                 "com.sun.jdo.spi.persistence.support.sqlstore.impl.SQLPersistenceManagerFactory.pmCacheBucketSize", // NOI18N
110
11).intValue();
111
112         pmCacheInitialCapacity = Integer.getInteger(
113                 "com.sun.jdo.spi.persistence.support.sqlstore.impl.SQLPersistenceManagerFactory.pmCacheInitialCapacity", // NOI18N
114
11 * pmCacheBucketSize).intValue();
115
116         if (logger.isLoggable(Logger.FINEST)) {
117             logger.finest(
118                 "sqlstore.sqlpersistencemgrfactory.pmCacheBucketSize", // NOI18N
119
String.valueOf(pmCacheBucketSize));
120             logger.finest(
121                 "sqlstore.sqlpersistencemgrfactory.pmCacheInitialCapacity", // NOI18N
122
String.valueOf(pmCacheInitialCapacity));
123         }
124     }
125
126     /**
127      * Transactional cache of PersistenceManager instances
128      */

129     private Map JavaDoc pmCache = new BucketizedHashtable(pmCacheBucketSize,
130             pmCacheInitialCapacity);
131
132     /**
133      * Cache of StateManager instances that support version consistency
134      */

135     private VersionConsistencyCache vcCache = null;
136
137     /**
138      * Creates new <code>SQLPersistenceManagerFactory</code> without any user info
139      */

140     public SQLPersistenceManagerFactory() {
141
142     }
143
144     /**
145      * Creates new <code>SQLPersistenceManagerFactory</code> with user info
146      * @param connectionFactory Connection Factory as java.lang.Object
147      */

148     public SQLPersistenceManagerFactory(Object JavaDoc connectionFactory) {
149         if (connectionFactory instanceof ConnectionFactory)
150             _connectionFactory = (ConnectionFactory) connectionFactory;
151         else
152             _dataSource = connectionFactory;
153
154         if (this instanceof PersistenceManagerFactory)
155             _persistenceManagerFactory = this;
156
157         initialize();
158     }
159
160     /**
161      * Creates new <code>SQLPersistenceManagerFactory</code> with user parameters
162      * @param persistenceManagerFactory PersistenceManagerFactory instance
163      */

164     public SQLPersistenceManagerFactory(
165             PersistenceManagerFactory persistenceManagerFactory) {
166         _persistenceManagerFactory = persistenceManagerFactory;
167         Object JavaDoc cf = _persistenceManagerFactory.getConnectionFactory();
168
169         if (cf instanceof ConnectionFactory)
170             _connectionFactory = (ConnectionFactory) cf;
171         else
172             _dataSource = cf;
173
174         initialize();
175     }
176
177     /**
178      * Sets database user name
179      * @param database user name
180      */

181     public void setConnectionUserName(String JavaDoc userName) {
182         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
183                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
184
}
185
186     /**
187      * Returns database user name
188      * @return current database user name
189      */

190     public String JavaDoc getConnectionUserName() {
191         return _persistenceManagerFactory.getConnectionUserName();
192     }
193
194     /**
195      * Sets database user password
196      * @param database user password
197      */

198     public void setConnectionPassword(String JavaDoc password) {
199         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
200                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
201
}
202
203     /**
204      * Sets connection URL
205      * @param connection URL
206      */

207     public void setConnectionURL(String JavaDoc url) {
208         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
209                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
210
}
211
212     /**
213      * Returns connection URL
214      * @return connection URL
215      */

216     public String JavaDoc getConnectionURL() {
217         return _persistenceManagerFactory.getConnectionURL();
218
219     }
220
221     /**
222      * Sets JDBC driver name
223      * @param driver name
224      */

225     public void setConnectionDriverName(String JavaDoc driverName) {
226         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
227                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
228
}
229
230     /**
231      * Returns JDBC driver name
232      * @return driver name
233      */

234     public String JavaDoc getConnectionDriverName() {
235         return _persistenceManagerFactory.getConnectionDriverName();
236
237     }
238
239     /**
240      * Sets ConnectionFactory
241      * @param ConnectionFactory as java.lang.Object
242      */

243     public void setConnectionFactory(Object JavaDoc cf) {
244         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
245                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
246
}
247
248     /**
249      * Returns ConnectionFactory
250      * @return Connection Factory as java.lang.Object
251      */

252     public Object JavaDoc getConnectionFactory() {
253         if (_dataSource != null)
254             return _dataSource;
255
256         return _connectionFactory;
257     }
258
259     /**
260      * Sets the optimistic flag for all PersistenceManagers
261      * @param flag boolean optimistic flag
262      */

263     public void setOptimistic(boolean flag) {
264         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
265                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
266
}
267
268     /**
269      * Returns the boolean value of the optimistic flag for all PersistenceManagers
270      * @return boolean optimistic flag
271      */

272     public boolean getOptimistic() {
273         return _persistenceManagerFactory.getOptimistic();
274     }
275
276     /**
277      * Sets flag that will not cause the eviction of persistent instances after transaction completion.
278      * @param flag boolean flag passed
279      */

280     public void setRetainValues(boolean flag) {
281         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
282                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
283
}
284
285     /**
286      * Returns the boolean value for the flag that will not cause the eviction of persistent
287      * instances after transaction completion.
288      * @return boolean setting for the flag
289      */

290     public boolean getRetainValues() {
291         return _persistenceManagerFactory.getRetainValues();
292     }
293
294     /**
295      * Sets the flag that allows non-transactional instances to be managed in the cache.
296      * @param flag boolean flag passed
297      */

298     public void setNontransactionalRead(boolean flag) {
299         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
300                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
301
}
302
303     /**
304      * Returns the boolean value for the flag that allows non-transactional instances to be
305      * managed in the cache.
306      * @return boolean setting for the flag
307      */

308     public boolean getNontransactionalRead() {
309         return _persistenceManagerFactory.getNontransactionalRead();
310     }
311
312
313     /**
314      * Sets the flag that allows the user to request that queries be optimized to return
315      * approximate results by ignoring changed values in the cache.
316      * @param flag boolean flag passed
317      */

318     public void setIgnoreCache(boolean flag) {
319         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
320                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
321
}
322
323     /**
324      * Returns the boolean value for the flag that allows the user to request that queries
325      * be optimized to return approximate results by ignoring changed values in the cache.
326      * @return boolean setting for the flag
327      */

328     public boolean getIgnoreCache() {
329         return _persistenceManagerFactory.getIgnoreCache();
330     }
331
332     /**
333      * Sets the number of seconds to wait for a query statement
334      * to execute in the datastore associated with this PersistenceManagerFactory.
335      * @param timeout new timout value in seconds; zero means unlimited
336      */

337     public void setQueryTimeout(int timeout) {
338         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
339                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
340
}
341
342     /**
343      * Gets the number of seconds to wait for a query statement
344      * to execute in the datastore associated with this PersistenceManagerFactory.
345      * @return timout value in seconds; zero means unlimited
346      */

347     public int getQueryTimeout() {
348         return _persistenceManagerFactory.getQueryTimeout();
349     }
350
351     /**
352      * Sets maximum number of connections in the connection pool
353      * @param MaxPool maximum number of connections
354      */

355     public void setConnectionMaxPool(int MaxPool) {
356         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
357                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
358
}
359
360     /**
361      * Returns maximum number of connections in the connection pool
362      * @return connectionMaxPool
363      */

364     public int getConnectionMaxPool() {
365         return _persistenceManagerFactory.getConnectionMaxPool();
366     }
367
368     /**
369      * Sets minimum number of connections in the connection pool
370      * @param MinPool minimum number of connections
371      */

372     public void setConnectionMinPool(int MinPool) {
373         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
374                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
375
}
376
377     /**
378      * Returns minimum number of connections in the connection pool
379      * @return connectionMinPool
380      */

381     public int getConnectionMinPool() {
382         return _persistenceManagerFactory.getConnectionMinPool();
383     }
384
385     /**
386      * Sets the number of milliseconds to wait for an available connection
387      * from the connection pool before throwing an exception
388      * @param MsWait number in milliseconds
389      */

390     public void setConnectionMsWait(int MsWait) {
391         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
392                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
393
}
394
395     /**
396      * Returns the number of milliseconds to wait for an available connection
397      * from the connection pool before throwing an exception
398      * @return number in milliseconds
399      */

400     public int getConnectionMsWait() {
401         return _persistenceManagerFactory.getConnectionMsWait();
402     }
403
404     /**
405      * Sets the amount of time, in milliseconds, between the connection
406      * manager's attempts to get a pooled connection.
407      * @param MsInterval the interval between attempts to get a database
408      * connection, in milliseconds.
409      *
410      */

411     public void setConnectionMsInterval(int MsInterval) {
412         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
413                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
414
}
415
416     /**
417      * Returns the amount of time, in milliseconds, between the connection
418      * manager's attempts to get a pooled connection.
419      * @return the length of the interval between tries in milliseconds
420      */

421     public int getConnectionMsInterval() {
422         return _persistenceManagerFactory.getConnectionMsInterval();
423     }
424
425     /**
426      * Sets the number of seconds to wait for a new connection to be
427      * established to the data source
428      * @param LoginTimeout wait time in seconds
429      */

430     public void setConnectionLoginTimeout(int LoginTimeout) {
431         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
432                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
433
}
434
435     /**
436      * Returns the number of seconds to wait for a new connection to be
437      * established to the data source
438      * @return wait time in seconds
439      */

440     public int getConnectionLoginTimeout() {
441         return _persistenceManagerFactory.getConnectionLoginTimeout();
442     }
443
444     /**
445      * Sets the LogWriter to which messages should be sent
446      * @param pw LogWriter
447      */

448     public void setConnectionLogWriter(PrintWriter JavaDoc pw) {
449         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
450                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
451
}
452
453     /**
454      * Returns the LogWriter to which messages should be sent
455      * @return LogWriter
456      */

457     public PrintWriter JavaDoc getConnectionLogWriter() {
458         return _persistenceManagerFactory.getConnectionLogWriter();
459     }
460
461     /**
462      * Sets transaction isolation level for all connections of this PersistenceManagerFactory.
463      * All validation is done by java.sql.Connection itself, so e.g. while Oracle
464      * will not allow to set solation level to TRANSACTION_REPEATABLE_READ, this method
465      * does not have any explicit restrictions
466      *
467      * @param level - one of the java.sql.Connection.TRANSACTION_* isolation values
468      */

469     public void setConnectionTransactionIsolation(int level) {
470         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
471                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
472
}
473
474     /**
475      * Returns current transaction isolation level for connections of this PersistenceManagerFactory.
476      * @return the current transaction isolation mode value as java.sql.Connection.TRANSACTION_*
477      */

478     public int getConnectionTransactionIsolation() {
479         return _persistenceManagerFactory.getConnectionTransactionIsolation();
480     }
481
482     /**
483      * Sets ConnectionFactory name
484      * @param connectionFactoryName ConnectionFactory name
485      */

486     public void setConnectionFactoryName(String JavaDoc connectionFactoryName) {
487         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
488                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
489
}
490
491     /**
492      * Returns ConnectionFactory name
493      * @return ConnectionFactoryName
494      */

495     public String JavaDoc getConnectionFactoryName() {
496         return _persistenceManagerFactory.getConnectionFactoryName();
497     }
498
499     /**
500      * Sets Identifier.
501      * @param identifier
502      */

503     public void setIdentifier(String JavaDoc identifier) {
504         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
505                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
506
}
507
508     /**
509      * Gets Identifier.
510      * @return identifier
511      */

512     public String JavaDoc getIdentifier() {
513         return _persistenceManagerFactory.getIdentifier();
514     }
515
516     /**
517      * Returns maximum number of PersistenceManager instances in the pool
518      * @return maxPool
519      */

520     public int getMaxPool() {
521         return maxPool;
522     }
523
524
525     /**
526      * Sets maximum number of PersistenceManager instances in the pool
527      * @param MaxPool maximum number of PersistenceManager instances
528      */

529     public void setMaxPool(int MaxPool) {
530         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
531                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
532
}
533
534     /**
535      * Returns minimum number of PersistenceManager instances in the pool
536      * @return minPool
537      */

538     public int getMinPool() {
539         return minPool;
540     }
541
542
543     /**
544      * Sets minimum number of PersistenceManager instances in the pool
545      * @param MinPool minimum number of PersistenceManager instances
546      */

547     public void setMinPool(int MinPool) {
548         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
549                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
550
}
551
552
553     /**
554      * Sets the number of seconds to wait for an update statement
555      * to execute in the datastore associated with this PersistenceManagerFactory.
556      * @param timeout new timout value in seconds; zero means unlimited
557      */

558     public void setUpdateTimeout(int timeout) {
559         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
560                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
561
}
562
563     /**
564      * Gets the number of seconds to wait for an update statement
565      * to execute in the datastore associated with this PersistenceManagerFactory.
566      * @return timout value in seconds; zero means unlimited
567      */

568     public int getUpdateTimeout() {
569         return _persistenceManagerFactory.getUpdateTimeout();
570     }
571
572     /**
573      * Returns the boolean value of the supersedeDeletedInstance flag
574      * for all PersistenceManagers. If set to true, deleted instances are
575      * allowed to be replaced with persistent-new instances with the equal
576      * Object Id.
577      * @return boolean supersedeDeletedInstance flag
578      */

579     public boolean getSupersedeDeletedInstance () {
580         return _persistenceManagerFactory.getSupersedeDeletedInstance();
581     }
582
583
584     /**
585      * Sets the supersedeDeletedInstance flag for all PersistenceManagers.
586      * @param flag boolean supersedeDeletedInstance flag
587      */

588     public void setSupersedeDeletedInstance (boolean flag) {
589         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
590                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
591
}
592
593     /**
594       * Returns the default value of the requireCopyObjectId flag. If set to false, PersistenceManagers
595       * will not create a copy of an ObjectId for <code>PersistenceManager.getObjectId(Object pc)</code>
596       * and <code>PersistenceManager.getObjectById(Object oid)</code> requests.
597       *
598       * @see PersistenceManager#getObjectId(Object pc)
599       * @see PersistenceManager#getObjectById(Object oid)
600       * @return boolean requireCopyObjectId flag
601       */

602      public boolean getRequireCopyObjectId() {
603          return _persistenceManagerFactory.getRequireCopyObjectId();
604      }
605
606
607      /**
608       * Sets the default value of the requireCopyObjectId.
609       * If set to false, PersistenceManagers will not create a copy of
610       * an ObjectId for <code>PersistenceManager.getObjectId(Object pc)</code>
611       * and <code>PersistenceManager.getObjectById(Object oid)</code> requests.
612       *
613       * @see PersistenceManager#getObjectId(Object pc)
614       * @see PersistenceManager#getObjectById(Object oid)
615       * @param flag boolean requireCopyObjectId flag
616       */

617      public void setRequireCopyObjectId (boolean flag) {
618          throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
619                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
620
}
621
622     /**
623      * Returns the boolean value of the requireTrackedSCO flag
624      * for this PersistenceManagerFactory. If set to false, by default the
625      * PersistenceManager will not create tracked SCO instances for
626      * new persistent instances at commit with retainValues set to true and while
627      * retrieving data from a datastore.
628      *
629      * @return boolean requireTrackedSCO flag
630      */

631     public boolean getRequireTrackedSCO() {
632         return _persistenceManagerFactory.getRequireTrackedSCO();
633     }
634   
635     /**
636      * Sets the requireTrackedSCO flag for this PersistenceManagerFactory.
637      * If set to false, by default the PersistenceManager will not create tracked
638      * SCO instances for new persistent instances at commit with retainValues set to true
639      * requests and while retrieving data from a datastore.
640      *
641      * @param flag boolean requireTrackedSCO flag
642      */

643     public void setRequireTrackedSCO (boolean flag) {
644         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
645                 "jdo.persistencemanagerfactoryimpl.notsupported")); //NOI18N
646
}
647
648     /**
649      * Creates new <a HREF="PersistenceManager.html">PersistenceManager</a> without specific
650      * info.
651      * @return the persistence manager
652      * @exception JDOUserException if data source info is not set
653      */

654     public PersistenceManager getPersistenceManager() {
655         return getPersistenceManager(null, null);
656     }
657
658     /**
659      * Creates new <a HREF="PersistenceManager.html">PersistenceManager</a> with specific
660      * username and password. Used to call ConnectionFactory.getConnection(String, String)
661      * @param username datasource user
662      * @param password datasource user password
663      * @return the persistence manager
664      * @exception JDOUserException if data source info is not set
665      */

666     public PersistenceManager getPersistenceManager(String JavaDoc username, String JavaDoc password) {
667         boolean debug = logger.isLoggable(Logger.FINEST);
668
669         if (_connectionFactory == null && _dataSource == null) {
670             throw new JDOUserException(I18NHelper.getMessage(messages,
671                     "jdo.persistencemanagerfactoryimpl.getpersistencemanager.notconfigured"));// NOI18N
672
}
673
674         if (debug) {
675              logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr",Thread.currentThread()); // NOI18N
676
}
677
678         // Check if we are in managed environment and PersistenceManager is cached
679
PersistenceManagerImpl pm = null;
680         javax.transaction.Transaction JavaDoc t = EJBHelper.getTransaction();
681
682         if (t != null) {
683             if (debug) {
684                 Object JavaDoc[] items = new Object JavaDoc[] {Thread.currentThread(),t};
685                 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.found",items); // NOI18N
686
}
687
688             pm = (PersistenceManagerImpl) pmCache.get(t);
689             if (pm == null) {
690                 // Not found
691
pm = getFromPool(t, username, password);
692                 pmCache.put(t, pm);
693             } else if(pm.isClosed()) {
694                 if (debug) {
695                      Object JavaDoc[] items = new Object JavaDoc[] {Thread.currentThread(),t};
696                      logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.pmclosedfor",items); // NOI18N
697
}
698                 throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
699                         "jdo.persistencemanagerfactoryimpl.getpersistencemanager.closed", // NOI18N
700
t));
701             }
702
703             // We know we are in the managed environment and
704
// JTA transaction is active. We need to start
705
// JDO Transaction internally if it is not active.
706

707             com.sun.jdo.spi.persistence.support.sqlstore.Transaction tx =
708                     (com.sun.jdo.spi.persistence.support.sqlstore.Transaction) pm.currentTransaction();
709             if (debug) {
710                 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.jdotx",tx); // NOI18N
711
}
712
713             if (!tx.isActive()) {
714                 tx.begin(t);
715             }
716
717             if (!(pm.verify(username, password))) {
718                 ;
719                 throw new JDOUserException(I18NHelper.getMessage(messages,
720                         "jdo.persistencemanagerfactoryimpl.getpersistencemanager.error")); // NOI18N
721
}
722         } else {
723             if (debug) {
724                 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.jdotx.notfound"); // NOI18N
725
}
726             // We don't know if we are in the managed environment or not
727
// If Yes, it is BMT with JDO Transaction and it will register
728
// itself at the begin().
729
pm = getFromPool(null, username, password);
730         }
731
732         if (debug) {
733             Object JavaDoc[] items = new Object JavaDoc[] {Thread.currentThread(),pm,t};
734             logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.pmt",items); // NOI18N
735
}
736
737         // Always return a wrapper
738
return new PersistenceManagerWrapper(pm);
739
740     }
741
742     /**
743      * Returns non-operational properties to be available to the application via a Properties instance.
744      * @return Properties object
745      */

746     public Properties JavaDoc getProperties() {
747         return _persistenceManagerFactory.getProperties();
748     }
749
750     /**
751      * Returns instance of PersistenceManagerFactory
752      */

753     public PersistenceManagerFactory getPersistenceManagerFactory() {
754         return _persistenceManagerFactory;
755     }
756
757     /**
758      * Registers PersistenceManager in the transactional cache in
759      * managed environment in case of BMT with JDO Transaction.
760      * There is no javax.transaction.Transaction
761      * available before the user starts the transaction.
762      */

763     public void registerPersistenceManager(
764             com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager pm,
765             javax.transaction.Transaction JavaDoc t) {
766
767         boolean debug = logger.isLoggable(Logger.FINEST);
768         if (debug) {
769             Object JavaDoc[] items = new Object JavaDoc[] {pm,t};
770             logger.finest("sqlstore.sqlpersistencemgrfactory.registerpersistencemgr.pmt",items); // NOI18N
771
}
772         PersistenceManager pm1 = (PersistenceManager) pmCache.get(t);
773         // double-check locking has been removed
774
if (pm1 == null) {
775             pmCache.put(t, pm);
776             ((PersistenceManagerImpl) pm).setJTATransaction(t);
777             return;
778         }
779
780         if (pm1 != pm) {
781             Object JavaDoc[] items = new Object JavaDoc[] {t, pm1};
782             throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
783                     "jdo.persistencemanagerfactoryimpl.registerpm.registered", // NOI18N
784
items));
785         } else {
786             // do nothing ???
787
}
788     }
789
790     /**
791      * Returns an instance of PersistenceManagerImpl from available pool
792      * or creates a new one
793      */

794     private PersistenceManagerImpl getFromPool(javax.transaction.Transaction JavaDoc tx,
795                                                String JavaDoc username, String JavaDoc password) {
796
797         boolean debug = logger.isLoggable(Logger.FINEST);
798         if (debug) {
799             logger.finest("sqlstore.sqlpersistencemgrfactory.getfrompool"); // NOI18N
800
}
801
802         synchronized (this) {
803             if (_store == null) {
804                 initializeSQLStoreManager(username, password);
805             }
806         }
807
808         // create new PersistenceManager object and set its atributes
809
PersistenceManagerImpl pm = new PersistenceManagerImpl(this, tx, username, password);
810         pm.setStore(_store);
811         if (debug) {
812             Object JavaDoc[] items = new Object JavaDoc[] {pm,tx};
813             logger.finest("sqlstore.sqlpersistencemgrfactory.getfrompool.pmt",items); // NOI18N
814
}
815
816         return pm;
817
818     }
819     
820     /**
821      * Returns unused PersistenceManager to the free pool
822      */

823     private void returnToPool(PersistenceManager pm) {
824         // do nothing for now
825
logger.finest("sqlstore.sqlpersistencemgrfactory.returnToPool"); // NOI18N
826
}
827
828     /** Releases closed PersistenceManager that is not in use
829      */

830     public void releasePersistenceManager(com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager pm,
831                                           javax.transaction.Transaction JavaDoc t) {
832
833
834         boolean debug = logger.isLoggable(Logger.FINEST);
835         if (debug) {
836             Object JavaDoc[] items = new Object JavaDoc[] {pm,t};
837             logger.finest("sqlstore.sqlpersistencemgrfactory.releasepm.pmt",items); // NOI18N
838

839         }
840
841         if (t != null) {
842             // Managed environment
843
// Deregister only
844
PersistenceManager pm1 = (PersistenceManager) pmCache.get(t);
845             if (pm1 == null || pm1 != pm) {
846                 Object JavaDoc[] items = new Object JavaDoc[] {t, pm1};
847                 throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
848                         "jdo.persistencemanagerfactoryimpl.registerpm.registered", // NOI18N
849
items));
850             } else {
851                 pmCache.remove(t);
852             }
853         } else {
854             returnToPool(pm);
855         }
856     }
857
858     private void initialize() {
859
860         logger.finest("sqlstore.sqlpersistencemgrfactory.init"); // NOI18N
861
optimistic = _persistenceManagerFactory.getOptimistic();
862         retainValues = _persistenceManagerFactory.getRetainValues();
863         nontransactionalRead = _persistenceManagerFactory.getNontransactionalRead();
864         ignoreCache = _persistenceManagerFactory.getIgnoreCache();
865         queryTimeout = _persistenceManagerFactory.getQueryTimeout();
866         updateTimeout = _persistenceManagerFactory.getUpdateTimeout();
867         minPool = _persistenceManagerFactory.getMinPool();
868         maxPool = _persistenceManagerFactory.getMaxPool();
869     }
870
871
872     private void initializeSQLStoreManager(String JavaDoc username, String JavaDoc password) {
873         Connection JavaDoc conn = null;
874         try {
875              conn = getConnection(username, password);
876             if (conn != null) {
877                 _store = new SQLStoreManager(conn.getMetaData(),
878                             getIdentifier() );
879             }
880         } catch(Exception JavaDoc e) {
881             if (logger.isLoggable(Logger.WARNING)) {
882                 logger.log(Logger.WARNING, "jdo.sqlpersistencemanagerfactory.errorgettingDatabaseInfo", e); //NOI18N
883
}
884
885             if (e instanceof JDOException) {
886                 throw (JDOException) e;
887             } else {
888                 throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
889                         "core.configuration.getvendortypefailed"), e); // NOI18N
890
}
891         } finally {
892             if (conn != null) {
893                 try {
894                     conn.close();
895                 } catch(Exception JavaDoc ex) {}
896             }
897         }
898         
899     }
900     
901     /**
902      * Get Database connection for username and password.
903      */

904     private Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc{
905         Connection JavaDoc conn = null;
906
907         if (_connectionFactory != null) {
908             conn = _connectionFactory.getConnection();
909         } else if (EJBHelper.isManaged()) {
910             conn = EJBHelper.getConnection(_dataSource, username, password);
911         } else {
912             if (username == null) {
913                 conn = ((DataSource JavaDoc)_dataSource).getConnection();
914             } else {
915                 conn = ((DataSource JavaDoc)_dataSource).getConnection(username, password);
916             }
917         }
918
919         return conn;
920     }
921     
922     /**
923      * Determines whether obj is a SQLPersistenceManagerFactory with the same configuration
924      *
925      * @param obj The possibly null object to check.
926      * @return true if obj is equal to this SQLPersistenceManagerFactory; false otherwise.
927      */

928     public boolean equals(Object JavaDoc obj) {
929         if ((obj != null) && (obj instanceof SQLPersistenceManagerFactory)) {
930             SQLPersistenceManagerFactory pmf = (SQLPersistenceManagerFactory) obj;
931             return (pmf._persistenceManagerFactory.equals(this._persistenceManagerFactory));
932
933         }
934         return false;
935     }
936
937     /**
938      * Computes the hash code of this PersistenceManagerFactory.
939      *
940      * @return A hash code of the owning PersistenceManagerFactory as an int.
941      */

942     public int hashCode() {
943         return this._persistenceManagerFactory.hashCode();
944     }
945
946     /**
947      * Creates if necessary, and returns, this PMF's instance of its
948      * VersionConsistencyCache.
949      * @return This PMF's VersionConsistencyCache.
950      */

951     public VersionConsistencyCache getVersionConsistencyCache() {
952         if (null == vcCache) {
953             if (_store == null) {
954                 // Store should be configured already.
955
throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
956                         "jdo.persistencemanagerfactoryimpl.getversionconsistencycache.nullstore")); // NOI18N
957
}
958             vcCache = VersionConsistencyCacheImpl.create();
959             _store.getConfigCache().setVersionConsistencyCache(vcCache);
960         }
961         return vcCache;
962     }
963 }
964
Popular Tags