KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.hibernate.FlushMode;
26 import org.hibernate.Session;
27 import org.hibernate.ejb.HibernateEntityManager;
28
29 import org.springframework.jdbc.datasource.ConnectionHandle;
30 import org.springframework.jdbc.datasource.SimpleConnectionHandle;
31 import org.springframework.orm.jpa.DefaultJpaDialect;
32 import org.springframework.transaction.TransactionDefinition;
33 import org.springframework.transaction.TransactionException;
34
35 /**
36  * {@link org.springframework.orm.jpa.JpaDialect} implementation for
37  * Hibernate EntityManager. Developed and tested against Hibernate 3.2.
38  *
39  * @author Costin Leau
40  * @author Juergen Hoeller
41  * @since 2.0
42  */

43 public class HibernateJpaDialect extends DefaultJpaDialect {
44
45     public Object JavaDoc beginTransaction(EntityManager entityManager, TransactionDefinition definition)
46             throws PersistenceException, SQLException JavaDoc, TransactionException {
47
48         super.beginTransaction(entityManager, definition);
49         Session session = getSession(entityManager);
50         FlushMode flushMode = session.getFlushMode();
51         FlushMode previousFlushMode = null;
52         if (definition.isReadOnly()) {
53             // We should suppress flushing for a read-only transaction.
54
session.setFlushMode(FlushMode.MANUAL);
55             previousFlushMode = flushMode;
56         }
57         else {
58             // We need AUTO or COMMIT for a non-read-only transaction.
59
if (flushMode.lessThan(FlushMode.COMMIT)) {
60                 session.setFlushMode(FlushMode.AUTO);
61                 previousFlushMode = flushMode;
62             }
63         }
64         return new SessionTransactionData(session, previousFlushMode);
65     }
66
67     public void cleanupTransaction(Object JavaDoc transactionData) {
68         ((SessionTransactionData) transactionData).resetFlushMode();
69     }
70
71     @Override JavaDoc
72     public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
73             throws PersistenceException, SQLException JavaDoc {
74
75         Session session = getSession(entityManager);
76         Connection JavaDoc con = session.connection();
77         return (con != null ? new SimpleConnectionHandle(con) : null);
78     }
79
80     protected Session getSession(EntityManager em) {
81         return ((HibernateEntityManager) em).getSession();
82     }
83
84
85     private static class SessionTransactionData {
86
87         private final Session session;
88
89         private final FlushMode previousFlushMode;
90
91         public SessionTransactionData(Session session, FlushMode previousFlushMode) {
92             this.session = session;
93             this.previousFlushMode = previousFlushMode;
94         }
95
96         public void resetFlushMode() {
97             if (this.previousFlushMode != null) {
98                 this.session.setFlushMode(this.previousFlushMode);
99             }
100         }
101     }
102
103 }
104
Popular Tags