This version of the manual is outdated. For the latest version, follow this link: http://www.jooq.org/doc/3.0/manual.
| The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : The Factory class | previous : next |
# The Factory and the jOOQ API
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
- Interface-driven design. This allows for modelling queries in a fluent API most efficiently
- Reduction of complexity for client code.
- API guarantee. You only depend on the exposed interfaces, not concrete (potentially dialect-specific) implementations.
The org.jooq.impl.Factory class is the main class from where you will create all jOOQ objects. The Factory implements org.jooq.Configuration and needs to be instanciated with the Configuration's properties:
- org.jooq.SQLDialect : The dialect of your database. This may be any of the currently supported database types
- java.sql.Connection : An optional JDBC Connection that will be re-used for the whole lifecycle of your Factory
- java.sql.DataSource : An optional JDBC DataSource that will be re-used for the whole lifecycle of your Factory. If you prefer using DataSources over Connections, jOOQ will internally fetch new Connections from your DataSource, conveniently closing them again after query execution. This is particularly useful in J2EE or Spring contexts.
- org.jooq.conf.Settings : An optional runtime configuration.
If you are planning on using several RDBMS (= SQLDialects) or several distinct JDBC Connections in your software, this will mean that you have to create a new Factory every time.
# Factory settings
The jOOQ Factory allows for some optional configuration elements to be used by advanced users. The Settings class is a JAXB-annotated type, that can be provided to a Factory in several ways:
- In the constructor. This will override default settings below
- From a location specified by a JVM parameter: -Dorg.jooq.settings
- From the classpath at /jooq-settings.xml
- From the settings defaults, as specified in http://www.jooq.org/xsd/jooq-runtime-2.1.0.xsd
Subsequent sections of the manual contain some more in-depth explanations about these settings:
Please refer to the jOOQ runtime configuration XSD for more details:
http://www.jooq.org/xsd/jooq-runtime-2.1.0.xsd
# Factory subclasses
There are a couple of subclasses for the general Factory. Each SQL dialect has its own dialect-specific factory. For instance, if you're only using the MySQL dialect, you can choose to create a new Factory using any of the following types:
// A general, dialect-unspecific factory Factory create = new Factory(connection, SQLDialect.MYSQL); // A MySQL-specific factory MySQLFactory create = new MySQLFactory(connection);
The advantage of using a dialect-specific Factory lies in the fact, that you have access to more proprietary RDMBS functionality. This may include:
- Oracle's CONNECT BY pseudo columns and functions
- MySQL's encryption functions
- PL/SQL constructs, pgplsql, or any other dialect's ROUTINE-language (maybe in the future)
Another type of Factory subclasses are each generated schema's factories. If you generate your schema TEST, then you will have access to a TestFactory. This will be useful in the future, when access to schema artefacts will be unified. Currently, this has no use.
# Static Factory methods
With jOOQ 2.0, static factory methods have been introduced in order to make your code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the Factory class:
import static org.jooq.impl.Factory.*;
This will allow to access functions even more fluently:
concat(trim(FIRST_NAME), trim(LAST_NAME)); // ... which is in fact the same as: Factory.concat(Factory.trim(FIRST_NAME), Factory.trim(LAST_NAME));
Objects created statically from the Factory do not need a reference to any factory, as they can be constructed independently from your Configuration (connection, dialect, schema mapping). They will access that information at render / bind time. See more details on the QueryParts' internals
# Potential problems
The jOOQ Factory expects its underlying java.sql.Connection to be open and ready for java.sql.PreparedStatement creation. You are responsible yourself for the lifecycle dependency between Factory and Connection. This means:
- jOOQ will never close the Connection.
- jOOQ will never commit or rollback on the Connection (Except for CSV-imports, if explicitly configured in the Import API)
- jOOQ will never start any transactions.
- jOOQ does not know the concept of a session as for instance Hibernate
- jOOQ does not know the concept of a second-level cache. SQL is executed directly on the underlying RDBMS.
- jOOQ does not make assumptions about the origin of the Connection. If it is container managed, that is fine.
So if you want your queries to run in separate transactions, if you want to roll back a transaction, if you want to close a Connection and return it to your container, you will have to take care of that yourself. jOOQ's Factory will always expect its Connection to be in a ready state for creating new PreparedStatements. If it is not, you have to create a new Factory.
Please keep in mind that many jOOQ objects will reference your Factory for their whole lifecycle. This is especially interesting, when dealing with Updatable Records, that can perform CRUD operations on the Factory's underlying Connection.
| The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : The Factory class | previous : next |
