KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > connection > DatasourceConnectionProvider


1 //$Id: DatasourceConnectionProvider.java,v 1.3 2005/07/15 00:01:40 epbernard Exp $
2
package org.hibernate.connection;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import javax.sql.DataSource JavaDoc;
9
10 import org.hibernate.HibernateException;
11 import org.hibernate.cfg.Environment;
12 import org.hibernate.util.NamingHelper;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 /**
18  * A connection provider that uses a <tt>DataSource</tt> registered with JNDI.
19  * Hibernate will use this <tt>ConnectionProvider</tt> by default if the
20  * property <tt>hibernate.connection.datasource</tt> is set.
21  * @see ConnectionProvider
22  * @author Gavin King
23  */

24 public class DatasourceConnectionProvider implements ConnectionProvider {
25     private DataSource JavaDoc ds;
26     private String JavaDoc user;
27     private String JavaDoc pass;
28
29     private static final Log log = LogFactory.getLog(DatasourceConnectionProvider.class);
30
31     protected DataSource JavaDoc getDataSource() {
32         return ds;
33     }
34
35     protected void setDataSource(DataSource JavaDoc ds) {
36         this.ds = ds;
37     }
38
39     public void configure(Properties JavaDoc props) throws HibernateException {
40
41         String JavaDoc jndiName = props.getProperty(Environment.DATASOURCE);
42         if (jndiName==null) {
43             String JavaDoc msg = "datasource JNDI name was not specified by property " + Environment.DATASOURCE;
44             log.fatal(msg);
45             throw new HibernateException(msg);
46         }
47
48         user = props.getProperty(Environment.USER);
49         pass = props.getProperty(Environment.PASS);
50
51         try {
52             ds = (DataSource JavaDoc) NamingHelper.getInitialContext(props).lookup(jndiName);
53         }
54         catch (Exception JavaDoc e) {
55             log.fatal( "Could not find datasource: " + jndiName, e );
56             throw new HibernateException( "Could not find datasource", e );
57         }
58         if (ds==null) throw new HibernateException( "Could not find datasource: " + jndiName );
59         log.info( "Using datasource: " + jndiName );
60     }
61
62     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
63         if (user != null || pass != null) {
64             return ds.getConnection(user, pass);
65         }
66         else {
67             return ds.getConnection();
68         }
69     }
70
71     public void closeConnection(Connection JavaDoc conn) throws SQLException JavaDoc {
72         conn.close();
73     }
74
75     public void close() {}
76
77     /**
78      * @see ConnectionProvider#supportsAggressiveRelease()
79      */

80     public boolean supportsAggressiveRelease() {
81         return true;
82     }
83
84 }
85
86
87
88
89
90
91
92
Popular Tags