All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.j256.ormlite.stmt.ThreadLocalSelectArg Maven / Gradle / Ivy

Go to download

Lightweight Object Relational Model (ORM) for persisting objects to SQL databases.

There is a newer version: 6.1
Show newest version
package com.j256.ormlite.stmt;

import com.j256.ormlite.field.SqlType;

/**
 * Like {@link SelectArg} but using a {@link ThreadLocal} internally to improve reentrance so that multiple threads can
 * use the same compiled statement.
 * 
 * @author graywatson
 */
public class ThreadLocalSelectArg extends BaseArgumentHolder {

	private ThreadLocal threadValue = new ThreadLocal();

	public ThreadLocalSelectArg() {
		super();
		// value set later
	}

	public ThreadLocalSelectArg(String columnName, Object value) {
		super(columnName);
		setValue(value);
	}

	public ThreadLocalSelectArg(SqlType sqlType, Object value) {
		super(sqlType);
		setValue(value);
	}

	public ThreadLocalSelectArg(Object value) {
		setValue(value);
	}

	@Override
	protected Object getValue() {
		ValueWrapper wrapper = threadValue.get();
		if (wrapper == null) {
			return null;
		} else {
			return wrapper.value;
		}
	}

	@Override
	public void setValue(Object value) {
		threadValue.set(new ValueWrapper(value));
	}

	@Override
	protected boolean isValueSet() {
		return threadValue.get() != null;
	}

	/**
	 * Value wrapper so we can differentiate between a null value and no value. Since we need to do this on a per thread
	 * basis, this can't be a volatile field or something.
	 */
	private static class ValueWrapper {
		Object value;
		public ValueWrapper(Object value) {
			this.value = value;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy