rendered paste body/** * */package db.sqlserver;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.DriverPropertyInfo;import java.sql.SQLException;import java.util.Properties;import play.Logger;import play.Play;import play.PlayPlugin;import db.sqlserver.DB;import play.exceptions.DatabaseException;import com.mchange.v2.c3p0.ComboPooledDataSource;/** * * @author eknowit * */public class DBPlugin extends PlayPlugin { @Override public void onApplicationStart() { try { Properties properties = Play.configuration; if (DB.datasource != null) { DB.destroy(); } // Probamos el driver String driver = properties.getProperty("sqlserver.db.driver"); try { Driver d = (Driver) Class.forName(driver, true, Play.classloader).newInstance(); DriverManager.registerDriver(new ProxyDriver(d)); } catch (Exception e) { throw new Exception("Driver not found (" + driver + ")"); } // Probamos la conexion Connection fake = null; try { if (properties.getProperty("sqlserver.db.user") == null) { fake = DriverManager.getConnection(properties.getProperty("sqlserver.db.url")); } else { fake = DriverManager.getConnection(properties.getProperty("sqlserver.db.url"), properties.getProperty("sqlserver.db.user"), properties.getProperty("sqlserver.db.pass")); } } finally { if (fake != null) { fake.close(); } } ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass(properties.getProperty("sqlserver.db.driver")); ds.setJdbcUrl(properties.getProperty("sqlserver.db.url")); ds.setUser(properties.getProperty("sqlserver.db.user")); ds.setPassword(properties.getProperty("sqlserver.db.pass")); ds.setAcquireRetryAttempts(10); ds.setCheckoutTimeout(Integer.parseInt(properties.getProperty("sqlserver.db.pool.timeout", "5000"))); ds.setBreakAfterAcquireFailure(false); ds.setMaxPoolSize(Integer.parseInt(properties.getProperty("sqlserver.db.pool.maxSize", "30"))); ds.setMinPoolSize(Integer.parseInt(properties.getProperty("sqlserver.db.pool.minSize", "1"))); ds.setMaxIdleTimeExcessConnections(Integer.parseInt(properties.getProperty("sqlserver.db.pool.maxIdleTimeExcessConnections", "0"))); ds.setIdleConnectionTestPeriod(10); ds.setTestConnectionOnCheckin(true); DB.datasource = ds; Connection c = null; try { c = ds.getConnection(); } finally { if (c != null) { c.close(); } } DB.destroyMethod = properties.getProperty("sqlserver.db.destroyMethod", ""); } catch (Exception e) { DB.datasource = null; Logger.error(e, "Cannot connected to the database : %s", e.getMessage()); if (e.getCause() instanceof InterruptedException) { throw new DatabaseException("Cannot connected to the database. Check the configuration.", e); } throw new DatabaseException("Cannot connected to the database, " + e.getMessage(), e); } } @Override public void onApplicationStop() { if (Play.mode.isProd()) { DB.destroy(); } } public static class ProxyDriver implements Driver { private Driver driver; ProxyDriver(Driver d) { this.driver = d; } public boolean acceptsURL(String u) throws SQLException { return this.driver.acceptsURL(u); } public Connection connect(String u, Properties p) throws SQLException { return this.driver.connect(u, p); } public int getMajorVersion() { return this.driver.getMajorVersion(); } public int getMinorVersion() { return this.driver.getMinorVersion(); } public DriverPropertyInfo[] getPropertyInfo(String u, Properties p) throws SQLException { return this.driver.getPropertyInfo(u, p); } public boolean jdbcCompliant() { return this.driver.jdbcCompliant(); } }}