Source Code Cross Referenced for JpaTransactionManager.java in  » J2EE » spring-framework-2.0.6 » org » springframework » orm » jpa » 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 » J2EE » spring framework 2.0.6 » org.springframework.orm.jpa 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.orm.jpa;
018:
019:        import java.util.HashMap;
020:        import java.util.Map;
021:        import java.util.Properties;
022:
023:        import javax.persistence.EntityManager;
024:        import javax.persistence.EntityManagerFactory;
025:        import javax.persistence.EntityTransaction;
026:        import javax.persistence.PersistenceException;
027:        import javax.persistence.RollbackException;
028:        import javax.sql.DataSource;
029:
030:        import org.springframework.beans.factory.InitializingBean;
031:        import org.springframework.dao.support.DataAccessUtils;
032:        import org.springframework.jdbc.datasource.ConnectionHandle;
033:        import org.springframework.jdbc.datasource.ConnectionHolder;
034:        import org.springframework.jdbc.datasource.JdbcTransactionObjectSupport;
035:        import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
036:        import org.springframework.transaction.CannotCreateTransactionException;
037:        import org.springframework.transaction.IllegalTransactionStateException;
038:        import org.springframework.transaction.TransactionDefinition;
039:        import org.springframework.transaction.TransactionException;
040:        import org.springframework.transaction.TransactionSystemException;
041:        import org.springframework.transaction.UnexpectedRollbackException;
042:        import org.springframework.transaction.support.AbstractPlatformTransactionManager;
043:        import org.springframework.transaction.support.DefaultTransactionStatus;
044:        import org.springframework.transaction.support.ResourceTransactionManager;
045:        import org.springframework.transaction.support.TransactionSynchronizationManager;
046:        import org.springframework.util.CollectionUtils;
047:
048:        /**
049:         * {@link org.springframework.transaction.PlatformTransactionManager} implementation
050:         * for a single JPA {@link javax.persistence.EntityManagerFactory}. Binds a JPA
051:         * EntityManager from the specified factory to the thread, potentially allowing for
052:         * one thread-bound EntityManager per factory. {@link SharedEntityManagerCreator}
053:         * and {@link JpaTemplate} are aware of thread-bound entity managers and participate
054:         * in such transactions automatically. Using either is required for JPA access code
055:         * supporting this transaction management mechanism.
056:         *
057:         * <p>This transaction manager is appropriate for applications that use a single
058:         * JPA EntityManagerFactory for  transactional data access. JTA (usually through
059:         * {@link org.springframework.transaction.jta.JtaTransactionManager}) is necessary
060:         * for accessing multiple transactional resources within the same transaction.
061:         * Note that you need to configure your JPA provider accordingly in order to make
062:         * it participate in JTA transactions.
063:         *
064:         * <p>This transaction manager also supports direct DataSource access within a
065:         * transaction (i.e. plain JDBC code working with the same DataSource).
066:         * This allows for mixing services which access JPA and services which use plain
067:         * JDBC (without being aware of JPA)! Application code needs to stick to the
068:         * same simple Connection lookup pattern as with
069:         * {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
070:         * (i.e. {@link org.springframework.jdbc.datasource.DataSourceUtils#getConnection}
071:         * or going through a
072:         * {@link org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy}).
073:         * Note that this requires a vendor-specific {@link JpaDialect} to be configured.
074:         *
075:         * <p>Note: To be able to register a DataSource's Connection for plain JDBC code,
076:         * this instance needs to be aware of the DataSource ({@link #setDataSource}).
077:         * The given DataSource should obviously match the one used by the given
078:         * EntityManagerFactory. This transaction manager will autodetect the DataSource
079:         * used as known connection factory of the EntityManagerFactory, so you usually
080:         * don't need to explicitly specify the "dataSource" property.
081:         *
082:         * <p>On JDBC 3.0, this transaction manager supports nested transactions via JDBC 3.0
083:         * Savepoints. The {@link #setNestedTransactionAllowed} "nestedTransactionAllowed"}
084:         * flag defaults to "false", though, as nested transactions will just apply to the
085:         * JDBC Connection, not to the JPA EntityManager and its cached objects.
086:         * You can manually set the flag to "true" if you want to use nested transactions
087:         * for JDBC access code which participates in JPA transactions (provided that your
088:         * JDBC driver supports Savepoints). <i>Note that JPA itself does not support
089:         * nested transactions! Hence, do not expect JPA access code to semantically
090:         * participate in a nested transaction.</i>
091:         *
092:         * @author Juergen Hoeller
093:         * @since 2.0
094:         * @see #setEntityManagerFactory
095:         * @see #setDataSource
096:         * @see LocalEntityManagerFactoryBean
097:         * @see JpaTemplate#execute
098:         * @see org.springframework.orm.jpa.support.SharedEntityManagerBean
099:         * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
100:         * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
101:         * @see org.springframework.jdbc.core.JdbcTemplate
102:         * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
103:         * @see org.springframework.transaction.jta.JtaTransactionManager
104:         */
105:        public class JpaTransactionManager extends
106:                AbstractPlatformTransactionManager implements 
107:                ResourceTransactionManager, InitializingBean {
108:
109:            private EntityManagerFactory entityManagerFactory;
110:
111:            private final Map jpaPropertyMap = new HashMap();
112:
113:            private DataSource dataSource;
114:
115:            private JpaDialect jpaDialect = new DefaultJpaDialect();
116:
117:            /**
118:             * Create a new JpaTransactionManager instance.
119:             * A EntityManagerFactory has to be set to be able to use it.
120:             * @see #setEntityManagerFactory
121:             */
122:            public JpaTransactionManager() {
123:            }
124:
125:            /**
126:             * Create a new JpaTransactionManager instance.
127:             * @param emf EntityManagerFactory to manage transactions for
128:             */
129:            public JpaTransactionManager(EntityManagerFactory emf) {
130:                this .entityManagerFactory = emf;
131:                afterPropertiesSet();
132:            }
133:
134:            /**
135:             * Set the EntityManagerFactory that this instance should manage transactions for.
136:             */
137:            public void setEntityManagerFactory(EntityManagerFactory emf) {
138:                this .entityManagerFactory = emf;
139:            }
140:
141:            /**
142:             * Return the EntityManagerFactory that this instance should manage transactions for.
143:             */
144:            public EntityManagerFactory getEntityManagerFactory() {
145:                return this .entityManagerFactory;
146:            }
147:
148:            /**
149:             * Specify JPA properties, to be passed into
150:             * <code>EntityManagerFactory.createEntityManager(Map)</code> (if any).
151:             * <p>Can be populated with a String "value" (parsed via PropertiesEditor)
152:             * or a "props" element in XML bean definitions.
153:             * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
154:             */
155:            public void setJpaProperties(Properties jpaProperties) {
156:                CollectionUtils.mergePropertiesIntoMap(jpaProperties,
157:                        this .jpaPropertyMap);
158:            }
159:
160:            /**
161:             * Specify JPA properties as a Map, to be passed into
162:             * <code>EntityManagerFactory.createEntityManager(Map)</code> (if any).
163:             * <p>Can be populated with a "map" or "props" element in XML bean definitions.
164:             * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
165:             */
166:            public void setJpaPropertyMap(Map jpaProperties) {
167:                if (jpaProperties != null) {
168:                    this .jpaPropertyMap.putAll(jpaProperties);
169:                }
170:            }
171:
172:            /**
173:             * Allow Map access to the JPA properties to be passed to the persistence
174:             * provider, with the option to add or override specific entries.
175:             * <p>Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
176:             */
177:            public Map getJpaPropertyMap() {
178:                return this .jpaPropertyMap;
179:            }
180:
181:            /**
182:             * Set the JDBC DataSource that this instance should manage transactions for.
183:             * The DataSource should match the one used by the JPA EntityManagerFactory:
184:             * for example, you could specify the same JNDI DataSource for both.
185:             * <p>If the EntityManagerFactory uses a known DataSource as connection factory,
186:             * the DataSource will be autodetected: You can still explictly specify the
187:             * DataSource, but you don't need to in this case.
188:             * <p>A transactional JDBC Connection for this DataSource will be provided to
189:             * application code accessing this DataSource directly via DataSourceUtils
190:             * or JdbcTemplate. The Connection will be taken from the JPA EntityManager.
191:             * <p>Note that you need to use a JPA dialect for a specific JPA implementation
192:             * to allow for exposing JPA transactions as JDBC transactions.
193:             * <p>The DataSource specified here should be the target DataSource to manage
194:             * transactions for, not a TransactionAwareDataSourceProxy. Only data access
195:             * code may work with TransactionAwareDataSourceProxy, while the transaction
196:             * manager needs to work on the underlying target DataSource. If there's
197:             * nevertheless a TransactionAwareDataSourceProxy passed in, it will be
198:             * unwrapped to extract its target DataSource.
199:             * @see EntityManagerFactoryInfo#getDataSource()
200:             * @see #setJpaDialect
201:             * @see org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
202:             * @see org.springframework.jdbc.datasource.DataSourceUtils
203:             * @see org.springframework.jdbc.core.JdbcTemplate
204:             */
205:            public void setDataSource(DataSource dataSource) {
206:                if (dataSource instanceof  TransactionAwareDataSourceProxy) {
207:                    // If we got a TransactionAwareDataSourceProxy, we need to perform transactions
208:                    // for its underlying target DataSource, else data access code won't see
209:                    // properly exposed transactions (i.e. transactions for the target DataSource).
210:                    this .dataSource = ((TransactionAwareDataSourceProxy) dataSource)
211:                            .getTargetDataSource();
212:                } else {
213:                    this .dataSource = dataSource;
214:                }
215:            }
216:
217:            /**
218:             * Return the JDBC DataSource that this instance manages transactions for.
219:             */
220:            public DataSource getDataSource() {
221:                return this .dataSource;
222:            }
223:
224:            /**
225:             * Set the JPA dialect to use for this transaction manager.
226:             * Used for vendor-specific transaction management and JDBC connection exposure.
227:             * <p>If the EntityManagerFactory uses a known JpaDialect, it will be autodetected:
228:             * You can still explictly specify the DataSource, but you don't need to in this case.
229:             * <p>The dialect object can be used to retrieve the underlying JDBC connection
230:             * and thus allows for exposing JPA transactions as JDBC transactions.
231:             * @see EntityManagerFactoryInfo#getJpaDialect()
232:             * @see JpaDialect#beginTransaction
233:             * @see JpaDialect#getJdbcConnection
234:             */
235:            public void setJpaDialect(JpaDialect jpaDialect) {
236:                this .jpaDialect = (jpaDialect != null ? jpaDialect
237:                        : new DefaultJpaDialect());
238:            }
239:
240:            /**
241:             * Return the JPA dialect to use for this transaction manager.
242:             */
243:            public JpaDialect getJpaDialect() {
244:                return this .jpaDialect;
245:            }
246:
247:            /**
248:             * Eagerly initialize the JPA dialect, creating a default one
249:             * for the specified EntityManagerFactory if none set.
250:             * Auto-detect the EntityManagerFactory's DataSource, if any.
251:             */
252:            public void afterPropertiesSet() {
253:                if (getEntityManagerFactory() == null) {
254:                    throw new IllegalArgumentException(
255:                            "Property 'entityManagerFactory' is required");
256:                }
257:                if (getEntityManagerFactory() instanceof  EntityManagerFactoryInfo) {
258:                    EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) getEntityManagerFactory();
259:                    DataSource dataSource = emfInfo.getDataSource();
260:                    if (dataSource != null) {
261:                        setDataSource(dataSource);
262:                    }
263:                    JpaDialect jpaDialect = emfInfo.getJpaDialect();
264:                    if (jpaDialect != null) {
265:                        setJpaDialect(jpaDialect);
266:                    }
267:                }
268:            }
269:
270:            public Object getResourceFactory() {
271:                return getEntityManagerFactory();
272:            }
273:
274:            protected Object doGetTransaction() {
275:                JpaTransactionObject txObject = new JpaTransactionObject();
276:                txObject.setSavepointAllowed(isNestedTransactionAllowed());
277:
278:                EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
279:                        .getResource(getEntityManagerFactory());
280:                if (emHolder != null) {
281:                    if (logger.isDebugEnabled()) {
282:                        logger.debug("Found thread-bound EntityManager ["
283:                                + emHolder.getEntityManager()
284:                                + "] for JPA transaction");
285:                    }
286:                    txObject.setEntityManagerHolder(emHolder, false);
287:                }
288:
289:                if (getDataSource() != null) {
290:                    ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager
291:                            .getResource(getDataSource());
292:                    txObject.setConnectionHolder(conHolder);
293:                }
294:
295:                return txObject;
296:            }
297:
298:            protected boolean isExistingTransaction(Object transaction) {
299:                return ((JpaTransactionObject) transaction).hasTransaction();
300:            }
301:
302:            protected void doBegin(Object transaction,
303:                    TransactionDefinition definition) {
304:                JpaTransactionObject txObject = (JpaTransactionObject) transaction;
305:
306:                if (txObject.hasConnectionHolder()
307:                        && !txObject.getConnectionHolder()
308:                                .isSynchronizedWithTransaction()) {
309:                    throw new IllegalTransactionStateException(
310:                            "Pre-bound JDBC Connection found! JpaTransactionManager does not support "
311:                                    + "running within DataSourceTransactionManager if told to manage the DataSource itself. "
312:                                    + "It is recommended to use a single JpaTransactionManager for all transactions "
313:                                    + "on a single DataSource, no matter whether JPA or JDBC access.");
314:                }
315:
316:                EntityManager em = null;
317:
318:                try {
319:                    if (txObject.getEntityManagerHolder() == null
320:                            || txObject.getEntityManagerHolder()
321:                                    .isSynchronizedWithTransaction()) {
322:                        EntityManager newEm = createEntityManagerForTransaction();
323:                        if (logger.isDebugEnabled()) {
324:                            logger.debug("Opened new EntityManager [" + newEm
325:                                    + "] for JPA transaction");
326:                        }
327:                        txObject.setEntityManagerHolder(
328:                                new EntityManagerHolder(newEm), true);
329:                    }
330:
331:                    txObject.getEntityManagerHolder()
332:                            .setSynchronizedWithTransaction(true);
333:                    em = txObject.getEntityManagerHolder().getEntityManager();
334:
335:                    // Delegate to JpaDialect for actual transaction begin.
336:                    Object transactionData = getJpaDialect().beginTransaction(
337:                            em, definition);
338:                    txObject.setTransactionData(transactionData);
339:
340:                    // Register transaction timeout.
341:                    int timeout = determineTimeout(definition);
342:                    if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
343:                        txObject.getEntityManagerHolder().setTimeoutInSeconds(
344:                                timeout);
345:                    }
346:
347:                    // Register the JPA EntityManager's JDBC Connection for the DataSource, if set.
348:                    if (getDataSource() != null) {
349:                        ConnectionHandle conHandle = getJpaDialect()
350:                                .getJdbcConnection(em, definition.isReadOnly());
351:                        if (conHandle != null) {
352:                            ConnectionHolder conHolder = new ConnectionHolder(
353:                                    conHandle);
354:                            if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
355:                                conHolder.setTimeoutInSeconds(timeout);
356:                            }
357:                            if (logger.isDebugEnabled()) {
358:                                logger
359:                                        .debug("Exposing JPA transaction as JDBC transaction ["
360:                                                + conHolder
361:                                                        .getConnectionHandle()
362:                                                + "]");
363:                            }
364:                            TransactionSynchronizationManager.bindResource(
365:                                    getDataSource(), conHolder);
366:                            txObject.setConnectionHolder(conHolder);
367:                        } else {
368:                            if (logger.isDebugEnabled()) {
369:                                logger
370:                                        .debug("Not exposing JPA transaction ["
371:                                                + em
372:                                                + "] as JDBC transaction because JpaDialect ["
373:                                                + getJpaDialect()
374:                                                + "] does not support JDBC Connection retrieval");
375:                            }
376:                        }
377:                    }
378:
379:                    // Bind the entity manager holder to the thread.
380:                    if (txObject.isNewEntityManagerHolder()) {
381:                        TransactionSynchronizationManager.bindResource(
382:                                getEntityManagerFactory(), txObject
383:                                        .getEntityManagerHolder());
384:                    }
385:                }
386:
387:                catch (TransactionException ex) {
388:                    if (em != null) {
389:                        em.close();
390:                    }
391:                    throw ex;
392:                } catch (Exception ex) {
393:                    if (em != null) {
394:                        em.close();
395:                    }
396:                    throw new CannotCreateTransactionException(
397:                            "Could not open JPA EntityManager for transaction",
398:                            ex);
399:                }
400:            }
401:
402:            /**
403:             * Create a JPA EntityManager to be used for a transaction.
404:             * <p>The default implementation checks whether the EntityManagerFactory
405:             * is a Spring proxy and unwraps it first.
406:             * @see javax.persistence.EntityManagerFactory#createEntityManager()
407:             * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory()
408:             */
409:            protected EntityManager createEntityManagerForTransaction() {
410:                EntityManagerFactory emf = getEntityManagerFactory();
411:                if (emf instanceof  EntityManagerFactoryInfo) {
412:                    emf = ((EntityManagerFactoryInfo) emf)
413:                            .getNativeEntityManagerFactory();
414:                }
415:                Map properties = getJpaPropertyMap();
416:                return (!CollectionUtils.isEmpty(properties) ? emf
417:                        .createEntityManager(properties) : emf
418:                        .createEntityManager());
419:            }
420:
421:            protected Object doSuspend(Object transaction) {
422:                JpaTransactionObject txObject = (JpaTransactionObject) transaction;
423:                txObject.setEntityManagerHolder(null, false);
424:                EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager
425:                        .unbindResource(getEntityManagerFactory());
426:                txObject.setConnectionHolder(null);
427:                ConnectionHolder connectionHolder = null;
428:                if (getDataSource() != null) {
429:                    connectionHolder = (ConnectionHolder) TransactionSynchronizationManager
430:                            .unbindResource(getDataSource());
431:                }
432:                return new SuspendedResourcesHolder(entityManagerHolder,
433:                        connectionHolder);
434:            }
435:
436:            protected void doResume(Object transaction,
437:                    Object suspendedResources) {
438:                SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources;
439:                TransactionSynchronizationManager.bindResource(
440:                        getEntityManagerFactory(), resourcesHolder
441:                                .getEntityManagerHolder());
442:                if (getDataSource() != null) {
443:                    TransactionSynchronizationManager.bindResource(
444:                            getDataSource(), resourcesHolder
445:                                    .getConnectionHolder());
446:                }
447:            }
448:
449:            /**
450:             * This implementation returns "true": a JPA commit will properly handle
451:             * transactions that have been marked rollback-only at a global level.
452:             */
453:            protected boolean shouldCommitOnGlobalRollbackOnly() {
454:                return true;
455:            }
456:
457:            protected void doCommit(DefaultTransactionStatus status) {
458:                JpaTransactionObject txObject = (JpaTransactionObject) status
459:                        .getTransaction();
460:                if (status.isDebug()) {
461:                    logger
462:                            .debug("Committing JPA transaction on EntityManager ["
463:                                    + txObject.getEntityManagerHolder()
464:                                            .getEntityManager() + "]");
465:                }
466:                try {
467:                    EntityTransaction tx = txObject.getEntityManagerHolder()
468:                            .getEntityManager().getTransaction();
469:                    tx.commit();
470:                } catch (RollbackException ex) {
471:                    throw new UnexpectedRollbackException(
472:                            "JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation)",
473:                            ex);
474:                } catch (RuntimeException rawException) {
475:                    // Assumably failed to flush changes to database.
476:                    throw DataAccessUtils.translateIfNecessary(rawException,
477:                            getJpaDialect());
478:                }
479:            }
480:
481:            protected void doRollback(DefaultTransactionStatus status) {
482:                JpaTransactionObject txObject = (JpaTransactionObject) status
483:                        .getTransaction();
484:                if (status.isDebug()) {
485:                    logger
486:                            .debug("Rolling back JPA transaction on EntityManager ["
487:                                    + txObject.getEntityManagerHolder()
488:                                            .getEntityManager() + "]");
489:                }
490:                try {
491:                    EntityTransaction tx = txObject.getEntityManagerHolder()
492:                            .getEntityManager().getTransaction();
493:                    if (tx.isActive()) {
494:                        tx.rollback();
495:                    }
496:                } catch (PersistenceException ex) {
497:                    throw new TransactionSystemException(
498:                            "Could not roll back JPA transaction", ex);
499:                } finally {
500:                    if (!txObject.isNewEntityManagerHolder()) {
501:                        // Clear all pending inserts/updates/deletes in the EntityManager.
502:                        // Necessary for pre-bound EntityManagers, to avoid inconsistent state.
503:                        txObject.getEntityManagerHolder().getEntityManager()
504:                                .clear();
505:                    }
506:                }
507:            }
508:
509:            protected void doSetRollbackOnly(DefaultTransactionStatus status) {
510:                JpaTransactionObject txObject = (JpaTransactionObject) status
511:                        .getTransaction();
512:                if (status.isDebug()) {
513:                    logger.debug("Setting JPA transaction on EntityManager ["
514:                            + txObject.getEntityManagerHolder()
515:                                    .getEntityManager() + "] rollback-only");
516:                }
517:                txObject.setRollbackOnly();
518:            }
519:
520:            protected void doCleanupAfterCompletion(Object transaction) {
521:                JpaTransactionObject txObject = (JpaTransactionObject) transaction;
522:
523:                // Remove the entity manager holder from the thread.
524:                if (txObject.isNewEntityManagerHolder()) {
525:                    TransactionSynchronizationManager
526:                            .unbindResource(getEntityManagerFactory());
527:                }
528:                txObject.getEntityManagerHolder().clear();
529:
530:                // Remove the JDBC connection holder from the thread, if exposed.
531:                if (txObject.hasConnectionHolder()) {
532:                    TransactionSynchronizationManager
533:                            .unbindResource(getDataSource());
534:                    try {
535:                        getJpaDialect().releaseJdbcConnection(
536:                                txObject.getConnectionHolder()
537:                                        .getConnectionHandle(),
538:                                txObject.getEntityManagerHolder()
539:                                        .getEntityManager());
540:                    } catch (Exception ex) {
541:                        // Just log it, to keep a transaction-related exception.
542:                        logger
543:                                .error(
544:                                        "Could not close JDBC connection after transaction",
545:                                        ex);
546:                    }
547:                }
548:
549:                getJpaDialect().cleanupTransaction(
550:                        txObject.getTransactionData());
551:
552:                // Remove the entity manager holder from the thread.
553:                if (txObject.isNewEntityManagerHolder()) {
554:                    EntityManager em = txObject.getEntityManagerHolder()
555:                            .getEntityManager();
556:                    if (logger.isDebugEnabled()) {
557:                        logger.debug("Closing JPA EntityManager [" + em
558:                                + "] after transaction");
559:                    }
560:                    em.close();
561:                } else {
562:                    logger
563:                            .debug("Not closing pre-bound JPA EntityManager after transaction");
564:                }
565:            }
566:
567:            /**
568:             * JPA transaction object, representing a EntityManagerHolder.
569:             * Used as transaction object by JpaTransactionManager.
570:             *
571:             * <p>Derives from JdbcTransactionObjectSupport in order to inherit the
572:             * capability to manage JDBC 3.0 Savepoints for underlying JDBC Connections.
573:             *
574:             * @see EntityManagerHolder
575:             */
576:            private static class JpaTransactionObject extends
577:                    JdbcTransactionObjectSupport {
578:
579:                private EntityManagerHolder entityManagerHolder;
580:
581:                private boolean newEntityManagerHolder;
582:
583:                private Object transactionData;
584:
585:                public void setEntityManagerHolder(
586:                        EntityManagerHolder entityManagerHolder,
587:                        boolean newEntityManagerHolder) {
588:                    this .entityManagerHolder = entityManagerHolder;
589:                    this .newEntityManagerHolder = newEntityManagerHolder;
590:                }
591:
592:                public EntityManagerHolder getEntityManagerHolder() {
593:                    return this .entityManagerHolder;
594:                }
595:
596:                public boolean isNewEntityManagerHolder() {
597:                    return this .newEntityManagerHolder;
598:                }
599:
600:                public boolean hasTransaction() {
601:                    return (this .entityManagerHolder != null
602:                            && this .entityManagerHolder.getEntityManager() != null && this .entityManagerHolder
603:                            .getEntityManager().getTransaction().isActive());
604:                }
605:
606:                public void setTransactionData(Object transactionData) {
607:                    this .transactionData = transactionData;
608:                }
609:
610:                public Object getTransactionData() {
611:                    return this .transactionData;
612:                }
613:
614:                public void setRollbackOnly() {
615:                    EntityTransaction tx = this .entityManagerHolder
616:                            .getEntityManager().getTransaction();
617:                    if (tx.isActive()) {
618:                        tx.setRollbackOnly();
619:                    }
620:                    if (hasConnectionHolder()) {
621:                        getConnectionHolder().setRollbackOnly();
622:                    }
623:                }
624:
625:                public boolean isRollbackOnly() {
626:                    EntityTransaction tx = this .entityManagerHolder
627:                            .getEntityManager().getTransaction();
628:                    return tx.getRollbackOnly();
629:                }
630:            }
631:
632:            /**
633:             * Holder for suspended resources.
634:             * Used internally by <code>doSuspend</code> and <code>doResume</code>.
635:             */
636:            private static class SuspendedResourcesHolder {
637:
638:                private final EntityManagerHolder entityManagerHolder;
639:
640:                private final ConnectionHolder connectionHolder;
641:
642:                private SuspendedResourcesHolder(EntityManagerHolder emHolder,
643:                        ConnectionHolder conHolder) {
644:                    this .entityManagerHolder = emHolder;
645:                    this .connectionHolder = conHolder;
646:                }
647:
648:                private EntityManagerHolder getEntityManagerHolder() {
649:                    return this .entityManagerHolder;
650:                }
651:
652:                private ConnectionHolder getConnectionHolder() {
653:                    return this.connectionHolder;
654:                }
655:            }
656:
657:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.