1 package org.hibernate.connection; 3 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 import java.util.Properties ; 7 8 import javax.sql.DataSource ; 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 24 public class DatasourceConnectionProvider implements ConnectionProvider { 25 private DataSource ds; 26 private String user; 27 private String pass; 28 29 private static final Log log = LogFactory.getLog(DatasourceConnectionProvider.class); 30 31 protected DataSource getDataSource() { 32 return ds; 33 } 34 35 protected void setDataSource(DataSource ds) { 36 this.ds = ds; 37 } 38 39 public void configure(Properties props) throws HibernateException { 40 41 String jndiName = props.getProperty(Environment.DATASOURCE); 42 if (jndiName==null) { 43 String 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 ) NamingHelper.getInitialContext(props).lookup(jndiName); 53 } 54 catch (Exception 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 getConnection() throws SQLException { 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 conn) throws SQLException { 72 conn.close(); 73 } 74 75 public void close() {} 76 77 80 public boolean supportsAggressiveRelease() { 81 return true; 82 } 83 84 } 85 86 87 88 89 90 91 92 | Popular Tags |