本文整理匯總了Java中org.apache.commons.dbcp2.BasicDataSource類的典型用法代碼示例。如果您正苦於以下問題:Java BasicDataSource類的具體用法?Java BasicDataSource怎麽用?Java BasicDataSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BasicDataSource類屬於org.apache.commons.dbcp2包,在下文中一共展示了BasicDataSource類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: newEntityManagerFactory
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
public static EntityManagerFactory newEntityManagerFactory(DataSource dataSource, Class<?> ... entityPackages){
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
BasicDataSource basicDataSource = (BasicDataSource)dataSource;
Database jpaDatabase = DatabaseType.findTypeByJdbcUrl(basicDataSource.getUrl()).getJpaDatabase();
vendorAdapter.setDatabase(jpaDatabase);
HashMap<String, Object> properties = new HashMap<String, Object>();
//properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
//import @Entity classes
factory.setPackagesToScan(Arrays.stream(entityPackages).map(thing -> thing.getPackage().getName()).toArray(String[]::new));
//factory.setPersistenceUnitName("jproject");
factory.setDataSource(dataSource);
factory.setJpaPropertyMap(properties);
factory.afterPropertiesSet();
return factory.getObject();
}
示例2: herdDataSource
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
/**
* Get a new herd data source based on an in-memory HSQLDB database. This data source is used for loading the configuration table as an environment property
* source as well as for the JPA entity manager. It will therefore create/re-create the configuration table which is required for the former. It also
* inserts required values for both scenarios.
*
* @return the test herd data source.
*/
@Bean
public static DataSource herdDataSource()
{
// Create and return a data source that can connect directly to a JDBC URL.
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(org.h2.Driver.class.getName());
basicDataSource.setUsername("");
basicDataSource.setPassword("");
basicDataSource.setUrl("jdbc:h2:mem:herdTestDb");
// Create and populate the configuration table.
// This is needed for all data source method calls since it is used to create the environment property source which happens before
// JPA and other non-static beans are initialized.
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("createConfigurationTableAndData.sql"));
DatabasePopulatorUtils.execute(resourceDatabasePopulator, basicDataSource); // This is what the DataSourceInitializer does.
return basicDataSource;
}
示例3: test
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
@Test
public void test() throws SQLException {
BasicDataSource dataSource = getDataSource("");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE employee (id INTEGER)");
dataSource.close();
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(1, finishedSpans.size());
checkTags(finishedSpans, "myservice", "jdbc:hsqldb:mem:spring");
checkSameTrace(finishedSpans);
assertNull(mockTracer.scopeManager().active());
}
示例4: testWithSpanOnlyWithParent
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
@Test
public void testWithSpanOnlyWithParent() throws SQLException {
try (Scope activeSpan = mockTracer.buildSpan("parent").startActive(true)) {
BasicDataSource dataSource = getDataSource(";traceWithActiveSpanOnly=true");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE with_parent_skip (id INTEGER)");
dataSource.close();
}
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(2, finishedSpans.size());
checkSameTrace(finishedSpans);
assertNull(mockTracer.scopeManager().active());
}
示例5: getBasicDataSource
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
private static BasicDataSource getBasicDataSource(DatasourceConfiguration configuration) {
BasicDataSource dbcpDataSource = new BasicDataSource();
dbcpDataSource.setDriverClassName(configuration.getDriverClassname());
dbcpDataSource.setUrl(configuration.getUrl());
dbcpDataSource.setUsername(configuration.getUser());
dbcpDataSource.setPassword(configuration.getPassword());
// Enable statement caching (Optional)
dbcpDataSource.setPoolPreparedStatements(true);
dbcpDataSource.setValidationQuery("Select 1 ");
dbcpDataSource.setMaxOpenPreparedStatements(50);
dbcpDataSource.setLifo(true);
dbcpDataSource.setMaxTotal(10);
dbcpDataSource.setInitialSize(2);
return dbcpDataSource;
}
示例6: testDecoratingCanBeDisabledForSpecificBeans
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
@Test
public void testDecoratingCanBeDisabledForSpecificBeans() throws Exception {
EnvironmentTestUtils.addEnvironment(context,
"decorator.datasource.exclude-beans:secondDataSource");
context.register(TestMultiDataSourceConfiguration.class,
DataSourceAutoConfiguration.class,
DataSourceDecoratorAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
context.refresh();
DataSource dataSource = context.getBean("dataSource", DataSource.class);
assertThat(dataSource).isInstanceOf(DecoratedDataSource.class);
DataSource secondDataSource = context.getBean("secondDataSource", DataSource.class);
assertThat(secondDataSource).isInstanceOf(BasicDataSource.class);
}
開發者ID:gavlyukovskiy,項目名稱:spring-boot-data-source-decorator,代碼行數:17,代碼來源:DataSourceDecoratorAutoConfigurationTests.java
示例7: dataSource
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
/**
* The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a
* directory called "jpaserver_derby_files".
*
* A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource.
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource retVal = new BasicDataSource();
/*
retVal.setDriver(new org.apache.derby.jdbc.EmbeddedDriver());
retVal.setUrl("jdbc:derby:directory:target/jpaserver_derby_files;create=true");
retVal.setUsername("");
retVal.setPassword("");
* */
try
{
retVal.setDriver(new com.mysql.jdbc.Driver());
}
catch (Exception exc)
{
exc.printStackTrace();
}
retVal.setUrl("jdbc:mysql://localhost:3306/dhis2_fhir");
retVal.setUsername("root");
retVal.setPassword("");
return retVal;
}
示例8: dataSource
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
/**
* The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a
* directory called "jpaserver_derby_files".
*
* A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource.
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource retVal = new BasicDataSource();
/*
retVal.setDriver(new org.apache.derby.jdbc.EmbeddedDriver());
retVal.setUrl("jdbc:derby:directory:target/jpaserver_derby_files;create=true");
retVal.setUsername("");
retVal.setPassword("");
*/
try
{
//retVal.setDriver(new com.mysql.jdbc.Driver());
retVal.setDriver(new org.postgresql.Driver());
}
catch (Exception exc)
{
exc.printStackTrace();
}
//retVal.setUrl("jdbc:mysql://localhost:3306/dhis2_fhir");
retVal.setUrl("jdbc:postgresql://localhost:5432/dhis2_fhir");
retVal.setUsername("fhir");
retVal.setPassword("xxxxxxx");
return retVal;
}
示例9: DBManager
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
public DBManager() {
ds = new BasicDataSource();
ds.setDriver(new EmbeddedDriver());
ds.setUrl(Constants.JDBC);
flyway = new Flyway();
flyway.setDataSource(ds);
//flyway.clean();
flyway.migrate();
// just to be sure, try to close
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
LOG.info("Closing DB connection...");
ds.close();
LOG.info("DB closed");
} catch (SQLException ex) {
LOG.error("Error closing DB cconnection", ex);
}
}
});
}
示例10: spring
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
@Test
public void spring() throws SQLException {
BasicDataSource dataSource = getDataSource(false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE employee (id INTEGER)");
dataSource.close();
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(1, finishedSpans.size());
MockSpan mockSpan = finishedSpans.get(0);
assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey()));
assertEquals(JdbcTracingUtils.COMPONENT_NAME, mockSpan.tags().get(Tags.COMPONENT.getKey()));
assertThat(mockSpan.tags().get(Tags.DB_STATEMENT.getKey()).toString()).isNotEmpty();
assertEquals("h2", mockSpan.tags().get(Tags.DB_TYPE.getKey()));
assertEquals("sa", mockSpan.tags().get(Tags.DB_USER.getKey()));
assertEquals(0, mockSpan.generatedErrors().size());
assertNull(mockTracer.activeSpan());
}
示例11: spring_with_parent
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
@Test
public void spring_with_parent() throws Exception {
try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
BasicDataSource dataSource = getDataSource(false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE with_parent_1 (id INTEGER)");
jdbcTemplate.execute("CREATE TABLE with_parent_2 (id INTEGER)");
dataSource.close();
}
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
checkSameTrace(spans);
}
示例12: spring_with_parent_and_active_span_only
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
@Test
public void spring_with_parent_and_active_span_only() throws Exception {
try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
BasicDataSource dataSource = getDataSource(true);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE with_parent_skip_1 (id INTEGER)");
jdbcTemplate.execute("CREATE TABLE with_parent_skip_2 (id INTEGER)");
dataSource.close();
}
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
checkSameTrace(spans);
}
示例13: getEntityManagerFactory
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
/**
* Returns the singleton EntityManagerFactory instance for accessing the
* default database.
*
* @return the singleton EntityManagerFactory instance
* @throws NamingException
* if a naming exception occurs during initialization
* @throws SQLException
* if a database occurs during initialization
* @throws IOException
*/
public static synchronized EntityManagerFactory getEntityManagerFactory()
throws NamingException, SQLException, IOException {
if (entityManagerFactory == null) {
InitialContext ctx = new InitialContext();
BasicDataSource ds = new BasicDataSource();
JsonNode credentials = readCredentialsFromEnvironment();
ds.setDriverClassName(credentials.get("driver").asText());
ds.setUrl(credentials.get("url").asText());
ds.setUsername(credentials.get("user").asText());
ds.setPassword(credentials.get("password").asText());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ds);
entityManagerFactory = Persistence.createEntityManagerFactory(
PERSISTENCE_UNIT_NAME, properties);
}
return entityManagerFactory;
}
示例14: getValue
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
public static String getValue(String databasePath, String table, String id)
{
if(!validate(table))
{ return null; }
Connection conn = null;
String val = null;
try
{
BasicDataSource ds = getDataSource(databasePath);
if(ds == null)
{ return val; }
conn = ds.getConnection();
PreparedStatement s = conn.prepareStatement("select json from " + table + " where id=?");
s.setString(1, id);
ResultSet rs = s.executeQuery();
if(rs.next())
{ val = rs.getString(1); }
rs.close();
s.close();
} catch(Exception e) {
// log?
} finally {
if(conn != null)
{ try { conn.close(); } catch(SQLException sqle) { } }
} return val;
}
示例15: invokeGetDataSource
import org.apache.commons.dbcp2.BasicDataSource; //導入依賴的package包/類
public DataSource invokeGetDataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://127.0.0.1:3306/inst02");
bds.setUsername("root");
bds.setPassword("123456");
bds.setMaxTotal(50);
bds.setInitialSize(20);
bds.setMaxWaitMillis(60000);
bds.setMinIdle(6);
bds.setLogAbandoned(true);
bds.setRemoveAbandonedOnBorrow(true);
bds.setRemoveAbandonedOnMaintenance(true);
bds.setRemoveAbandonedTimeout(1800);
bds.setTestWhileIdle(true);
bds.setTestOnBorrow(false);
bds.setTestOnReturn(false);
bds.setValidationQuery("select 'x' ");
bds.setValidationQueryTimeout(1);
bds.setTimeBetweenEvictionRunsMillis(30000);
bds.setNumTestsPerEvictionRun(20);
return bds;
}