1 package com.imcode.db; 2 3 import javax.sql.DataSource; 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 7 public class DataSourceDatabase implements Database { 8 9 private DataSource dataSource; 10 11 public DataSourceDatabase( DataSource dataSource ) { 12 this.dataSource = dataSource; 13 } 14 15 16 public Object execute( final DatabaseCommand databaseCommand ) throws DatabaseException { 17 try { 18 Connection connection = dataSource.getConnection(); 19 try { 20 DatabaseConnection defaultDatabaseConnection = new JdbcDatabaseConnection( connection ); 21 return databaseCommand.executeOn( defaultDatabaseConnection ); 22 } finally { 23 connection.close() ; 24 } 25 } catch ( SQLException e ) { 26 throw DatabaseException.fromSQLException( e ); 27 } 28 } 29 30 public Object executeCommand( final DatabaseCommand databaseCommand ) throws DatabaseException { 31 return execute(databaseCommand) ; 32 } 33 34 }