Java Code Examples for com.j256.ormlite.misc.IOUtils#closeQuietly()

The following examples show how to use com.j256.ormlite.misc.IOUtils#closeQuietly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseForeignCollection.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Uses the iterator to run through the dao and retain only the items that are in the passed in collection. This
 * will remove the items from the associated database table as well.
 * 
 * @return Returns true of the collection was changed at all otherwise false.
 */
@Override
public boolean retainAll(Collection<?> collection) {
	if (dao == null) {
		return false;
	}
	boolean changed = false;
	CloseableIterator<T> iterator = closeableIterator();
	try {
		while (iterator.hasNext()) {
			T data = iterator.next();
			if (!collection.contains(data)) {
				iterator.remove();
				changed = true;
			}
		}
		return changed;
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 2
Source File: BaseForeignCollection.java    From ormlite-core with ISC License 6 votes vote down vote up
/**
 * Clears the collection and uses the iterator to run through the dao and delete all of the items in the collection
 * from the associated database table. This is different from removing all of the elements in the table since this
 * iterator is across just one item's foreign objects.
 */
@Override
public void clear() {
	if (dao == null) {
		return;
	}
	CloseableIterator<T> iterator = closeableIterator();
	try {
		while (iterator.hasNext()) {
			iterator.next();
			iterator.remove();
		}
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 3
Source File: LazyForeignCollection.java    From ormlite-core with ISC License 6 votes vote down vote up
@Override
public boolean removeAll(Collection<?> collection) {
	boolean changed = false;
	CloseableIterator<T> iterator = iterator();
	try {
		while (iterator.hasNext()) {
			if (collection.contains(iterator.next())) {
				iterator.remove();
				changed = true;
			}
		}
		return changed;
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 4
Source File: EnumToStringTypeTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testEnumToStringValue() throws Exception {
	Class<LocalEnumToString> clazz = LocalEnumToString.class;
	Dao<LocalEnumToString, Object> dao = createDao(clazz, true);
	LocalEnumToString foo = new LocalEnumToString();
	foo.ourEnum = OurEnum.SECOND;
	assertEquals(1, dao.create(foo));
	GenericRawResults<String[]> results = dao.queryRaw("select * from " + TABLE_NAME);
	CloseableIterator<String[]> iterator = results.closeableIterator();
	try {
		assertTrue(iterator.hasNext());
		String[] result = iterator.next();
		assertNotNull(result);
		assertEquals(1, result.length);
		assertFalse(OurEnum.SECOND.name().equals(result[0]));
		assertTrue(OurEnum.SECOND.toString().equals(result[0]));
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 5
Source File: LazyForeignCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
@Override
public Object[] toArray() {
	List<T> items = new ArrayList<T>();
	CloseableIterator<T> iterator = iterator();
	try {
		while (iterator.hasNext()) {
			items.add(iterator.next());
		}
		return items.toArray();
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 6
Source File: LazyForeignCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
@Override
public boolean isEmpty() {
	CloseableIterator<T> iterator = iterator();
	try {
		return !iterator.hasNext();
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 7
Source File: LazyForeignCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
@Override
public int size() {
	CloseableIterator<T> iterator = iterator();
	try {
		int sizeC;
		for (sizeC = 0; iterator.hasNext(); sizeC++) {
			// move to next without constructing the object
			iterator.moveToNext();
		}
		return sizeC;
	} finally {
		IOUtils.closeQuietly(iterator);
	}
}
 
Example 8
Source File: LocalLog.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Read in our levels from our configuration file.
 */
static List<PatternLevel> readLevelResourceFile(InputStream stream) {
	List<PatternLevel> levels = null;
	if (stream != null) {
		try {
			levels = configureClassLevels(stream);
		} catch (IOException e) {
			System.err.println(
					"IO exception reading the log properties file '" + LOCAL_LOG_PROPERTIES_FILE + "': " + e);
		} finally {
			IOUtils.closeQuietly(stream);
		}
	}
	return levels;
}
 
Example 9
Source File: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testGetReadWriteNoInit() throws Exception {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource();
	try {
		pooled.getReadWriteConnection(null);
	} finally {
		IOUtils.closeQuietly(pooled);
	}
}
 
Example 10
Source File: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testReleaseNoInit() throws Exception {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource();
	try {
		pooled.releaseConnection(null);
	} finally {
		IOUtils.closeQuietly(pooled);
	}
}
 
Example 11
Source File: JdbcPooledConnectionSourceTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testClearTransaction() throws SQLException {
	JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource();
	pooled.initialize();
	try {
		pooled.clearSpecialConnection(null);
	} finally {
		IOUtils.closeQuietly(pooled);
	}
}
 
Example 12
Source File: H2DatabaseResults.java    From ormlite-core with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 13
Source File: H2ConnectionSource.java    From ormlite-core with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 14
Source File: AndroidDatabaseConnection.java    From ormlite-android with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 15
Source File: H2DatabaseConnection.java    From ormlite-core with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 16
Source File: JdbcCompiledStatement.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 17
Source File: JdbcDatabaseConnection.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 18
Source File: DataSourceConnectionSource.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}
 
Example 19
Source File: DataSourceConnectionSource.java    From ormlite-jdbc with ISC License 4 votes vote down vote up
/**
 * Initialize the class after the setters have been called. If you are using the no-arg constructor and Spring type
 * wiring, this should be called after all of the set methods.
 * 
 * @throws SQLException
 *             If the driver associated with the database URL is not found in the classpath.
 */
public void initialize() throws SQLException {
	if (initialized) {
		return;
	}
	if (dataSource == null) {
		throw new IllegalStateException("dataSource was never set on " + getClass().getSimpleName());
	}
	if (databaseType == null) {
		if (databaseUrl == null) {
			throw new IllegalStateException(
					"either the databaseUri or the databaseType must be set on " + getClass().getSimpleName());
		}
		databaseType = DatabaseTypeUtils.createDatabaseType(databaseUrl);
	}
	databaseType.loadDriver();
	if (databaseUrl != null) {
		databaseType.setDriver(DriverManager.getDriver(databaseUrl));
	}

	// see if we have a single connection data-source
	DatabaseConnection jdbcConn1 = null;
	DatabaseConnection jdbcConn2 = null;
	try {
		Connection conn1 = dataSource.getConnection();
		Connection conn2 = dataSource.getConnection();
		// sanity check for testing
		if (conn1 == null || conn2 == null) {
			isSingleConnection = true;
		} else {
			jdbcConn1 = new JdbcDatabaseConnection(conn1);
			jdbcConn2 = new JdbcDatabaseConnection(conn2);
			isSingleConnection = isSingleConnection(jdbcConn1, jdbcConn2);
		}
	} finally {
		IOUtils.closeQuietly(jdbcConn1);
		IOUtils.closeQuietly(jdbcConn2);
	}

	initialized = true;
}
 
Example 20
Source File: WrappedConnectionSource.java    From ormlite-core with ISC License 4 votes vote down vote up
@Override
public void closeQuietly() {
	IOUtils.closeQuietly(this);
}