1 21 22 package org.dbunit.database; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.sql.DataSource ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 30 38 public class DatabaseDataSourceConnection extends AbstractDatabaseConnection 39 implements IDatabaseConnection 40 { 41 private final String _schema; 42 private final DataSource _dataSource; 43 private final String _user; 44 private final String _password; 45 private Connection _connection; 46 47 public DatabaseDataSourceConnection(InitialContext context, String jndiName, 48 String schema) throws NamingException , SQLException 49 { 50 this((DataSource )context.lookup(jndiName), schema, null, null); 51 } 52 53 public DatabaseDataSourceConnection(InitialContext context, String jndiName, 54 String schema, String user, String password) 55 throws NamingException , SQLException 56 { 57 this((DataSource )context.lookup(jndiName), schema, user, password); 58 } 59 60 public DatabaseDataSourceConnection(InitialContext context, String jndiName) 61 throws NamingException , SQLException 62 { 63 this(context, jndiName, null); 64 } 65 66 public DatabaseDataSourceConnection(InitialContext context, String jndiName, 67 String user, String password) throws NamingException , SQLException 68 { 69 this(context, jndiName, null, user, password); 70 } 71 72 public DatabaseDataSourceConnection(DataSource dataSource) 73 throws SQLException 74 { 75 this(dataSource, null, null, null); 76 } 77 78 public DatabaseDataSourceConnection(DataSource dataSource, String user, 79 String password) throws SQLException 80 { 81 this(dataSource, null, user, password); 82 } 83 84 public DatabaseDataSourceConnection(DataSource dataSource, String schema) 85 throws SQLException 86 { 87 this(dataSource, schema, null, null); 88 } 89 90 public DatabaseDataSourceConnection(DataSource dataSource, String schema, 91 String user, String password) throws SQLException 92 { 93 _dataSource = dataSource; 94 _schema = schema; 95 _user = user; 96 _password = password; 97 } 98 99 102 public Connection getConnection() throws SQLException 103 { 104 if (_connection == null) 105 { 106 if (_user != null) 107 { 108 _connection = _dataSource.getConnection(_user, _password); 109 } 110 else 111 { 112 _connection = _dataSource.getConnection(); 113 } 114 } 115 return _connection; 116 } 117 118 public String getSchema() 119 { 120 return _schema; 121 } 122 123 public void close() throws SQLException 124 { 125 if (_connection != null) 126 { 127 _connection.close(); 128 _connection = null; 129 } 130 } 131 } 132 133 134 135 136 137 138 139 | Popular Tags |