KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.openjpa.persistence.OpenJPAEntityManager;
26
27 import org.springframework.jdbc.datasource.ConnectionHandle;
28 import org.springframework.jdbc.datasource.SimpleConnectionHandle;
29 import org.springframework.orm.jpa.DefaultJpaDialect;
30 import org.springframework.transaction.TransactionDefinition;
31 import org.springframework.transaction.TransactionException;
32
33 /**
34  * {@link org.springframework.orm.jpa.JpaDialect} implementation for
35  * Apache OpenJPA. Developed and tested against OpenJPA 0.9.6.
36  *
37  * @author Costin Leau
38  * @since 2.0
39  */

40 public class OpenJpaDialect extends DefaultJpaDialect {
41
42     @Override JavaDoc
43     public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
44             throws PersistenceException, SQLException JavaDoc {
45
46         Connection JavaDoc connection = (Connection JavaDoc) getOpenJPAEntityManager(entityManager).getConnection();
47         return new SimpleConnectionHandle(connection);
48     }
49
50     @Override JavaDoc
51     public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em)
52             throws PersistenceException, SQLException JavaDoc {
53
54         if (conHandle != null && conHandle.getConnection() != null) {
55             conHandle.getConnection().close();
56         }
57     }
58
59     @Override JavaDoc
60     public Object JavaDoc beginTransaction(EntityManager entityManager, TransactionDefinition definition)
61             throws PersistenceException, SQLException JavaDoc, TransactionException {
62
63         super.beginTransaction(entityManager, definition);
64         if (!definition.isReadOnly()) {
65             // Like with TopLink, make sure to start the logic transaction early so that other
66
// participants using the connection (such as JdbcTemplate) run in a transaction.
67
getOpenJPAEntityManager(entityManager).beginStore();
68         }
69         return null;
70     }
71
72     /**
73      * Return the OpenJPA-specific interface of <code>EntityManager</code>.
74      * @param em the generic <code>EntityManager</code> instance
75      * @return the OpenJPA-specific interface of <code>EntityManager</code>
76      */

77     protected OpenJPAEntityManager getOpenJPAEntityManager(EntityManager em) {
78         return (OpenJPAEntityManager) em;
79     }
80
81 }
82
Popular Tags