Other features

Overview of some smaller features of Midao JDBC

For detailed information and description of below processors - please look at JavaDoc.

Processors are responsible for processing/converting different types of information from/into Java Classes


1.1 QueryOutputProcessor

BasicQueryOutputProcessor

BasicQueryOutputProcessor is used (default) by OutputHandler to convert one types into another

Example usage
// for all new OutputHandler instances
MjdbcConfig.setDefaultQueryOutputProcessor(new BasicQueryOutputProcessor());
		  
// it is advised to use some singleton instead of creating new OutputProcessor instance every time.
OutputHandler output = new ArrayListOutputHandler(new BasicQueryOutputProcessor());
OutputHandler output = new ArrayOutputHandler(new BasicQueryOutputProcessor());
OutputHandler output = new BeanListOutputHandler(Class.class, new BasicQueryOutputProcessor());
OutputHandler output = new BeanMapOutputHandler(Class.class, new BasicQueryOutputProcessor());
OutputHandler output = new BeanOutputHandler(Class.class, new BasicQueryOutputProcessor());
OutputHandler output = new ColumnListOutputHandler(); // doesn't use QueryOutputHandler
OutputHandler output = new KeyedOutputHandler(new BasicQueryOutputProcessor());
OutputHandler output = new MapListOutputHandler(new BasicQueryOutputProcessor());
OutputHandler output = new MapOutputHandler(new BasicQueryOutputProcessor());
OutputHandler output = new RowCountOutputHandler(); // doesn't use QueryOutputHandler
OutputHandler output = new ScalarOutputHandler();// doesn't use QueryOutputHandler

1.2 QueryInputProcessor

BasicQueryInputProcessor

BasicQueryInputProcessor is used (default) by InputHandler to convert one types into another

Example usage
// for all new OutputHandler instances
MjdbcConfig.setDefaultQueryInputProcessor(new BasicQueryInputProcessor());
		  
// it is advised to use some singleton instead of creating new InputProcessor instance every time.
InputHandler input = new BeanInputHandler(new BasicQueryInputProcessor(), queryString, parameterValues, className/null);
InputHandler input = new BeanListInputHandler(new BasicQueryInputProcessor(), queryString, parameterValues);
InputHandler input = new MapInputHandler(new BasicQueryInputProcessor(), queryString, parameterValues, null);
InputHandler input = new MapListInputHandler(new BasicQueryInputProcessor(), queryString, parameterValues);

ExtendedQueryInputProcessor

ExtendedQueryInputProcessor is extended version of QueryInputProcessor and allows processing inline parameter configuration: SELECT name FROM students WHERE id = #{id,jdbcType=INTEGER,mode=in}. Further details can be found at Extended Input Handlers


SpringQueryInputProcessor

SpringQueryInputProcessor is used to provide support for Spring named parameters in queries for InputHandler

Example usage
// for all new OutputHandler instances
MjdbcConfig.setDefaultQueryInputProcessor(new SpringQueryInputProcessor());
		  
// it is advised to use some singleton instead of creating new InputProcessor instance every time.
InputHandler input = new BeanInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues, className/null);
InputHandler input = new BeanListInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues);
InputHandler input = new MapInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues, null);
InputHandler input = new MapListInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues);

// or

InputHandler input = new SpringBeanInputHandler(queryString, parameterValues);
InputHandler input = new SpringBeanListInputHandler(queryString, parameterValues);
InputHandler input = new SpringMapInputHandler(queryString, parameterValues);
InputHandler input = new SpringMapListInputHandler(queryString, parameterValues);

Midao JDBC allows plugging-in Database Connection Pool libraries such as: Apache DBCP and c3p0.

For detailed information and description of below Pooled Datasources - please look at JavaDoc.


Apache Commons DBCP

Database Connection Pool implementation from Apache

Example usage
pom.xml(Maven):
<!-- Java 1.6 -->
<dependency>
  <groupId>org.midao</groupId>
  <artifactId>midao-jdbc-dbcp</artifactId>
  <version>1.4.2</version>


  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>

<!-- Java 1.5 -->
<dependency>
  <groupId>org.midao</groupId>
  <artifactId>midao-jdbc-dbcp</artifactId>
  <version>1.3.2</version>


  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.3</version>
</dependency>


Java:

DataSource ds = MjdbcFactory.createDataSource(properties);
DataSource ds = MjdbcFactory.createDataSource(url);
DataSource ds = MjdbcFactory.createDataSource(url, userName, password);
DataSource ds = MjdbcFactory.createDataSource(driverClassName, url, userName, password);
DataSource ds = MjdbcFactory.createDataSource(driverClassName, url, userName, password, initialSize, maxActive);

C3P0

Database Connection Pool implementation from mchange

Example usage
pom.xml(Maven):

<dependency>
  <groupId>org.midao</groupId>
  <artifactId>midao-jdbc-c3p0</artifactId>
  <version>0.9.2</version>


  <groupId>com.mchange</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.2.1</version>
</dependency>

Java:

DataSource ds = MjdbcFactory.createDataSource(properties);
DataSource ds = MjdbcFactory.createDataSource(url);
DataSource ds = MjdbcFactory.createDataSource(url, userName, password);
DataSource ds = MjdbcFactory.createDataSource(driverClassName, url, userName, password);
DataSource ds = MjdbcFactory.createDataSource(driverClassName, url, userName, password, initialSize, maxActive);

For detailed information and description of below Profiler - please look at JavaDoc.

Profiler does just that - allows to perform Query Profiling.

Profiler

Base Profiler does basic profiling. By default profiling is switched off. Please look at the example below to find out how to use it

Example usage
// switch it on
MjdbcConfig.setProfilerEnabled(true);

// every instance we create via MjdbcFactory with Profiler enabled - it will be profiled.
QueryRunnerService runner = MjdbcFactory.getQueryRunner(this.dataSource);
			
// change Profiler output format
MjdbcConfig.setProfilerOutputFormat("Invoked class [%s].\n - Method [%s{}]\n - Args   [%s]\n - Time   [%5.3f] sec ");

// if existing QueryRunner instance need to use Profiler
QueryRunnerService queryRunnerWithProfiler = (QueryRunnerService) ProfilerFactory.newInstance(queryRunnerWithoutProfiler);

For detailed information and description of below handlers/processors - please look at JavaDoc.

Midao JDBC has support for named (NamedParameterJdbcTemplate etc.) parameters.


Spring input handlers

SpringQueryInputProcessor and Spring input handlers provide support for Spring named parameters in queries in Midao

Example usage
// for all new OutputHandler instances
MjdbcConfig.setDefaultQueryInputProcessor(new SpringQueryInputProcessor());
		  
// it is advised to use some singleton instead of creating new InputProcessor instance every time.
InputHandler input = new BeanInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues, className/null);
InputHandler input = new BeanListInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues);
InputHandler input = new MapInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues, null);
InputHandler input = new MapListInputHandler(new SpringQueryInputProcessor(), queryString, parameterValues);

// or

InputHandler input = new SpringBeanInputHandler(queryString, parameterValues);
InputHandler input = new SpringBeanListInputHandler(queryString, parameterValues);
InputHandler input = new SpringMapInputHandler(queryString, parameterValues);
InputHandler input = new SpringMapListInputHandler(queryString, parameterValues);

Spring exception handling

For full description please go to SpringExceptionHandler

For detailed information and description of below classes - please look at JavaDoc.

Midao started as improvement to Apache DBUtils. During development realised that the idea behind DbUtils and Midao is different and in order to impalement it - it should become independent project.

In order to ensure that existing knowledge of DbUtils could be reused and migration to Midao (from DbUtils) was as easy as possible - a lot of classes definition and functionality was preserved. Please be aware that during migration you need to update ResultSetHandler class names and all import package names

Midao offers the same QueryRunner/AsyncQueryRunner and very similar to ResultSetHandler - OutputHandler

It is generally advised to initialize QueryRunner via QueryRunnerService runner = MjdbcFactory.getQueryRunner(this.dataSource, BaseTypeHandler.class);

AsyncQueryRunner is no longer has any query execution logic and it is using QueryRunner to execute queries(supplied via Constructor/MjdbcFactory)

Midao offers support for Derby, PostgreSQL, MySQL (MariaDB), Microsoft SQL and Oracle.

It has Functional tests which ensure stable support and proper handling for those Databases

In Git-Hub under folder: midao-jdbc-core-jdbc4-test and midao-jdbc-core-jdbc3-test - you will be able to find Functional tests for all those Databases

In order to execute Functional tests on your Remote/Local Database - please modify midao--jdbc-core-jdbc*-test/test.properties and set all required Database parameters

Also please be aware that, in order to properly execute those tests, Oracle JDBC libraries (ojdbc5.jar/ojdbc6.jar) and Microsoft SQL JDBC libraries (sqljdbc.jar/sqljdbc6.jar) need to be downloaded and put under lib folder in those projects

By default Midao JDBC caches all queries and manager resources for you. In case big amount of data required to be read but should not be cached - lazy functionality can be used.

Please don't forget that while using lazy query execution - you are responsible for managing some of the resources. More details you will find below.

For information regarding scrollable/updateable lazy output please go to Lazy scroll/update support


For detailed description of LazyOutputHandler please go to Lazy Output handlers

Example usage
// It is generally advised to use standard cached queries as all resource management is done by the system.

// During lazy query execution developer have to manually manage transactions (QueryRunnerService.commit/rollback) and manually close resources (LazyOutputHandler.close).
QueryRunnerService runner = MjdbcFactory.getQueryRunner(this.dataSource);

// by default Midao JDBC uses LazyStatementHandler. If this was reconfigured use next line
QueryRunnerService runner = MjdbcFactory.getQueryRunner(this.dataSource, <TypeHandler.class/null>, LazyStatementHandler.class);

// lazy query execution can be done only in manual transaction mode.
runner.setTransactionManualMode(true);

MapInputHandler input = new MapInputHandler("SELECT * FROM students", null);

// return as Map List
MapLazyOutputHandler lazyMapList =  runner.query(input, new MapLazyOutputHandler());

// return as Bean List
BeanLazyOutputHandler<Student> lazyBeanList = runner.query(input, new BeanLazyOutputHandler<Student>(Student.class));

// iterate over Map List
while (lazyMapList.hasNext() == true) {
	// retrieve next element
	lazyMapList.getNext() // returns Map
}

// freeing all used resources. If not closed - might lead to resource leak and/or errors.
lazyMapList.close();

// iterate over Bean List
while (lazyBeanList.hasNext() == true) {
	// retrieve next element
	lazyBeanList.getNext() // returns Student class
}

// freeing all used resources. If not closed - might lead to resource leak and/or errors.
lazyBeanList.close();

// telling to finish current transaction(doesn't matter if it is update or just query) and free all the resources.
// if current transaction would be committed before LazyOutputHandler was closed - it might be no longer accessible.
runner.commit();

Cache size can be configured for QueryRunnerService:

Example of Lazy cache max size setting for QueryRunnerService
// set limit of 20 cached elements per one Lazy cache
runner.override(MjdbcConstants.OVERRIDE_LAZY_CACHE_MAX_SIZE, 20);

// specify to keep every element in cache
runner.override(MjdbcConstants.OVERRIDE_LAZY_CACHE_MAX_SIZE, -1);

or for the whole system:

Example of Lazy cache max size setting for whole system
// set limit of 20 cached elements per one Lazy cache
MjdbcConfig.setDefaultLazyCacheMaxSize(20);

// specify to keep every element in cache
MjdbcConfig.setDefaultLazyCacheMaxSize(-1);

Lazy scrollable and updateable lazy output handlers provide scroll and update functionality for lazy output handlers. This allows freely iterate over results and update them when needed.

Please be aware that not every JDBC Driver supports scroll/update the same way: Oracle, Microsoft SQL . Please refer to relevant JDBC Driver documentation and/or create POC to confirm support.


For detailed description of LazyOutputHandler please go to Lazy Output handlers

Example usage

// Please do not forget that all standard rules of LazyOutputHandlers still apply - using manual mode, closing resources and committing values (visit LazyOutputHandler link above).

// iterate over lazy cache
while (lazyScroll.hasNext() == true) {
	// retrieve next element
	lazyScroll.getNext();
}

// iterate backwards over lazy cache
while (lazyScroll.hasPrev() == true) {
	// retrieve prev element
	lazyScroll.hasPrev();
}

// move cursor to specified position
lazyScroll.moveTo(newCursorPosition);
// move cursor relatively by specified amount of rows
lazyScroll.moveRelative(moveCursorRelative);

// read value
lazyScroll.getCurrent();

// updates current row
lazyUpdate.updateRow(student);

// inserts new row
lazyUpdate.insertRow(student);

Changing default scroll type from TYPE_SCROLL_INSENSITIVE to TYPE_SCROLL_SENSITIVE. Can be used to provide support for Microsoft SQL. For details please visit Official Oracle JDBC tutorial (ResultSet)

Changing default TYPE_SCROLL_INSENSITIVE to TYPE_SCROLL_SENSITIVE
runner.override(MjdbcConstants.OVERRIDE_LAZY_SCROLL_CHANGE_SENSITIVE, true);

For detailed information and description of below classes - please look at JavaDoc.

List of available scrollable/updateable lazy output handlers:

  • BeanLazyScrollOutputHandler allows scroll over lazy output handler. Returns converted row as Java bean
  • BeanLazyUpdateOutputHandler allows update lazy output handler. Returns converted row as Java bean
  • BeanLazyScrollUpdateOutputHandler allows scroll over and update lazy output handler. Returns converted row as Java bean
  • MapLazyScrollOutputHandler allows scroll over lazy output handler. Returns converted row as Java map class
  • MapLazyUpdateOutputHandler allows update lazy output handler. Returns converted row as Java map class
  • MapLazyScrollUpdateOutputHandler allows scroll over and update lazy output handler. Returns converted row as Java map class

Below you can provide feedback, suggestions, questions regarding information posted on current page