/** * <p>Constructor for DataSourceProvider.</p> * * @param dataSource a {@link javax.sql.DataSource} object. * @param dialect a {@link de.mhus.lib.sql.Dialect} object. * @param config a {@link de.mhus.lib.core.directory.ResourceNode} object. * @param activator a {@link de.mhus.lib.core.MActivator} object. */ public DataSourceProvider(DataSource dataSource, Dialect dialect, IConfig config, MActivator activator) { doInitialize(config, activator); setDataSource(dataSource); setDialect(dialect); }
protected void updateStorageDataSource() throws InvalidSyntaxException, MException { Collection<ServiceReference<DataSource>> refs = context.getServiceReferences(DataSource.class, "(osgi.jndi.service.name="+storageDsName+")"); if (refs.size() == 0) throw new MException("datasource not found",storageDsName); storageDataSource = context.getService(refs.iterator().next()); storageDsProvider.setDataSource(storageDataSource); try { String driver = storageDataSource.getConnection().getMetaData().getDriverName(); Dialect dialect = Dialect.findDialect(driver); storageDsProvider.setDialect(dialect); } catch (Exception e) { log().e(e); } }
protected DbPool doCreateDataPool() { return new DefaultDbPool(new DataSourceProvider(getDataSource(), doCreateDialect(), doCreateConfig(), doCreateActivator() )); }
protected void updateArchiveDataSource() throws InvalidSyntaxException, MException { Collection<ServiceReference<DataSource>> refs = context.getServiceReferences(DataSource.class, "(osgi.jndi.service.name="+archiveDsName+")"); if (refs.size() == 0) throw new MException("datasource not found",archiveDsName); archiveDataSource = context.getService(refs.iterator().next()); archiveDsProvider.setDataSource(archiveDataSource); }
/** {@inheritDoc} */ @Override public Dialect getDialect() { if (dialect == null) { // default is mysql, can ignore sync! if (dataSource == null) return null; String driverName = null; try { Connection con = dataSource.getConnection(); driverName = con.getMetaData().getDriverName(); con.close(); } catch (Throwable t) { log().d(t); } dialect = Dialect.findDialect(driverName); log().i("found dialect", getName(), driverName, dialect); } return dialect; }
/** {@inheritDoc} */ @Override public InternalDbConnection createConnection() throws Exception { if (dataSource == null) return null; Connection con = null; try { con = dataSource.getConnection(); } catch (Exception e) { // special behavior for e.g. mysql, retry to get a connection after gc() // Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections if (e.getMessage() != null && e.getMessage().indexOf("Too many connections") > -1) { System.gc(); con = dataSource.getConnection(); } else throw e; } getDialect().prepareConnection(con); return new JdbcConnection(this, con); }
storageDsProvider = new DataSourceProvider(); updateStorageDataSource(); storagePool = new DefaultDbPool(storageDsProvider); config.archive = new MemoryStorage(); } else { archiveDsProvider = new DataSourceProvider(); updateArchiveDataSource(); archivePool = new DefaultDbPool(archiveDsProvider);