本文整理汇总了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;
}