List of usage examples for org.springframework.jdbc.datasource SingleConnectionDataSource destroy
@Override public void destroy()
From source file:springobjectmapper.Example.java
public static void main(String[] args) { // Dialect dialect = new HsqlDbDialect(); // SingleConnectionDataSource dataSource = new // SingleConnectionDataSource("jdbc:hsqldb:mem:test", "sa", "", false); Dialect dialect = new MySqlDialect(); SingleConnectionDataSource dataSource = new SingleConnectionDataSource("jdbc:mysql://localhost/foo", "root", "", false); SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource); template.update("CREATE TABLE country (id " + dialect.defaultIdFieldType() + " PRIMARY KEY, code CHAR(2) NOT NULL, name VARCHAR(50) NOT NULL)"); template.update("CREATE TABLE person (id " + dialect.defaultIdFieldType() + " PRIMARY KEY,first_name VARCHAR(50) NOT NULL,last_name VARCHAR(50) NOT NULL,country_id BIGINT NOT NULL,email VARCHAR(50) NOT NULL,FOREIGN KEY (country_id) REFERENCES country(id))"); PersonRepository persons = new PersonRepository(template, dialect); CountryRepository countries = new CountryRepository(template, dialect); Country uk = new Country("UK", "United Kingdom"); countries.save(uk);/*from w w w. j a va2 s. c o m*/ Country ussr = new Country("SU", "Soviet Union"); countries.save(ussr); persons.save(new Person("James", "Bond", "007@mi6.co.uk", uk)); persons.save(new Person("Tatiana", "Romanova", "tatiana.romanova@kgb.su", ussr)); persons.save(new Person("Alexander", "Trevelyan", "006@mi6.co.uk", uk)); System.out.println("Good guys:"); List<Person> results = persons .query(new Query("country_id=?", Order.by("last_name").descending(), uk.id())); for (Person person : results) { System.out.println(person.getFirstName() + " " + person.getLastName()); } System.out.println("Bad guys:"); results = persons.query(new Query("country_id!=?", Order.by("last_name").descending(), uk.id())); for (Person person : results) { System.out.println(person.getFirstName() + " " + person.getLastName()); } dataSource.destroy(); }
From source file:ru.adios.budgeter.BundleProvider.java
public static SingleConnectionDataSource setNewDatabase(String url, boolean destroyOld) { synchronized (BundleProvider.class) { final SingleConnectionDataSource ds = createDataSource(url); BundleContainer.BUNDLE.setNewDataSource(ds, new SpringTransactionalSupport(ds)); try {/*from w w w. j av a 2s .c om*/ final SingleConnectionDataSource ret = dataSource; if (destroyOld) { ret.destroy(); } return ret; } finally { dataSource = ds; } } }
From source file:com.emc.ecs.sync.EndToEndTest.java
protected void verifyDb(TestObjectSource testSource, boolean truncateDb) { SingleConnectionDataSource ds = new SingleConnectionDataSource(); ds.setUrl(SqliteDbService.JDBC_URL_BASE + dbFile.getPath()); ds.setSuppressClose(true);// ww w . java 2 s. c o m JdbcTemplate jdbcTemplate = new JdbcTemplate(ds); long totalCount = verifyDbObjects(jdbcTemplate, testSource.getObjects()); try { SqlRowSet rowSet = jdbcTemplate.queryForRowSet("SELECT count(source_id) FROM " + DbService.DEFAULT_OBJECTS_TABLE_NAME + " WHERE target_id != ''"); Assert.assertTrue(rowSet.next()); Assert.assertEquals(totalCount, rowSet.getLong(1)); if (truncateDb) jdbcTemplate.update("DELETE FROM " + DbService.DEFAULT_OBJECTS_TABLE_NAME); } finally { try { ds.destroy(); } catch (Throwable t) { log.warn("could not close datasource", t); } } }
From source file:org.agnitas.web.NewImportWizardAction.java
private void destroyTemporaryConnection(ImportRecipientsDao importRecipientsDao) { if (importRecipientsDao != null) { SingleConnectionDataSource temporaryConnection = importRecipientsDao.getTemporaryConnection(); if (temporaryConnection != null) { temporaryConnection.destroy(); importRecipientsDao.setTemporaryConnection(null); }//from w ww. ja v a 2s .c om } }