arkanovicz commented on code in PR #49: URL: https://github.com/apache/velocity-engine/pull/49#discussion_r1735194913
########## velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/CachingDatabaseObjectsFactory.java: ########## @@ -0,0 +1,203 @@ +package org.apache.velocity.runtime. resource.loader; + +import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; +import org.apache.commons.pool2.KeyedObjectPool; +import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.impl.DefaultPooledObject; +import org.apache.commons.pool2.impl.GenericKeyedObjectPool; +import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; +import org.apache.velocity.util.ExtProperties; +import org.slf4j.Logger; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * <p>Database objects factory which will keep a single connection to be able to cache statements preparation, by means + * of appropriate pools.</p> + * <p>This class requires the following optional dependency (maven syntax):</p> + * <pre><code> + * <dependency> + * <groupId>org.apache.commons</groupId> + * <artifactId>commons-pool2</artifactId> + * <version>2.12.0</version> + * <scope>runtime</scope> + * </dependency> + * </code></pre> + * <p>To use this class, you must add the following property to the example configuration described in + * @link{org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader} + * </p> + * <pre><code> + * resource.loader.ds.database_objects_factory.class = org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader<br> + * </code></pre> + * <p>The default size of each pool of prepared statements (there is one pool per statement) is 50. You can tune it + * with:</p> + * <pre><code> + * resource.loader.ds.database_objects_factory. = org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader<br> + * </code></pre> + * @see org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader + */ + +public class CachingDatabaseObjectsFactory implements DatabaseObjectsFactory { + + private static final String STATEMENTS_POOL_MAX_SIZE = "statements_pool_max_size"; + private static final int STATEMENTS_POOL_MAX_SIZE_DEFAULT = 50; + + private DataSource dataSource; + private Connection connection; + private int poolsMaxSize; + private KeyedObjectPool<String, PreparedStatement> statementsPool; + protected Logger log = null; + + private class PreparedStatementFactory extends BaseKeyedPooledObjectFactory<String, PreparedStatement> + { + @Override + public PreparedStatement create(String sql) throws Exception { + checkConnection(); + return connection.prepareStatement(sql); Review Comment: If the connection fails between `checkConnection()` and the following line, I guess we're just out of luck... Or maybe you had something else in mind? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org For additional commands, e-mail: dev-h...@velocity.apache.org