KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > vendor > TopLinkJpaDialect


1 /*
2  * Copyright 2002-2006 the original author or authors.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jpa.vendor;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.PersistenceException;
24
25 import oracle.toplink.essentials.internal.sessions.AbstractSession;
26 import oracle.toplink.essentials.sessions.Session;
27 import oracle.toplink.essentials.sessions.UnitOfWork;
28
29 import org.springframework.jdbc.datasource.ConnectionHandle;
30 import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
31 import org.springframework.jdbc.datasource.SimpleConnectionHandle;
32 import org.springframework.orm.jpa.DefaultJpaDialect;
33 import org.springframework.transaction.TransactionDefinition;
34 import org.springframework.transaction.TransactionException;
35
36 /**
37  * {@link org.springframework.orm.jpa.JpaDialect} implementation for
38  * Oracle TopLink Essentials. Developed and tested against TopLink Essentials v2.
39  *
40  * <p>By default, this class acquires a TopLink transaction to get the JDBC connection
41  * early. This allows mixing JDBC and JPA/TopLink operations in the same transaction.
42  * In some cases, this eager acquisition of a transaction/connection may impact
43  * scalability. In that case, set the "lazyDatabaseTransaction" flag to true if you
44  * do not require mixing JDBC and JPA operations in the same transaction. Otherwise,
45  * use a {@link org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy}
46  * to ensure that the cost of connection acquisition is near zero until code actually
47  * needs a JDBC Connection.
48  *
49  * @author Rod Johnson
50  * @author Juergen Hoeller
51  * @since 2.0
52  * @see #setLazyDatabaseTransaction
53  * @see LazyConnectionDataSourceProxy
54  */

55 public class TopLinkJpaDialect extends DefaultJpaDialect {
56     
57     private boolean lazyDatabaseTransaction = false;
58
59
60     /**
61      * Set whether to lazily start a database transaction within a TopLink
62      * transaction.
63      * <p>By default, database transactions are started early. This allows
64      * for reusing the same JDBC Connection throughout an entire transaction,
65      * including read operations, and also for exposing TopLink transactions
66      * to JDBC access code (working on the same DataSource).
67      * <p>It is only recommended to switch this flag to "true" when no JDBC access
68      * code is involved in any of the transactions, and when it is acceptable to
69      * perform read operations outside of the transactional JDBC Connection.
70      * @see oracle.toplink.sessions.UnitOfWork#beginEarlyTransaction()
71      */

72     public void setLazyDatabaseTransaction(boolean lazyDatabaseTransaction) {
73         this.lazyDatabaseTransaction = lazyDatabaseTransaction;
74     }
75
76
77     @Override JavaDoc
78     public Object JavaDoc beginTransaction(EntityManager entityManager, TransactionDefinition definition)
79             throws PersistenceException, SQLException JavaDoc, TransactionException {
80
81         super.beginTransaction(entityManager, definition);
82         if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
83             // This is the magic bit. As with the existing Spring TopLink integration,
84
// begin an early transaction to force TopLink to get a JDBC connection
85
// so that Spring can manage transactions with JDBC as well as TopLink.
86
UnitOfWork uow = (UnitOfWork) getSession(entityManager);
87             uow.beginEarlyTransaction();
88         }
89         // Could return the UOW, if there were any advantage in having it later.
90
return null;
91     }
92
93     @Override JavaDoc
94     public ConnectionHandle getJdbcConnection(EntityManager em, boolean readOnly)
95             throws PersistenceException, SQLException JavaDoc {
96
97         AbstractSession session = (AbstractSession) getSession(em);
98         // The connection was already acquired eagerly in beginTransaction,
99
// unless lazyDatabaseTransaction was set to true.
100
Connection JavaDoc con = session.getAccessor().getConnection();
101         return (con != null ? new SimpleConnectionHandle(con) : null);
102     }
103
104     /**
105      * Get a traditional TopLink Session from the given EntityManager.
106      */

107     protected Session getSession(EntityManager em) {
108         oracle.toplink.essentials.ejb.cmp3.EntityManager emi = (oracle.toplink.essentials.ejb.cmp3.EntityManager) em;
109         return emi.getActiveSession();
110     }
111
112 }
113
Popular Tags