|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 6
The benefits to defining resources inside the application are primarily with respect to ease-of-deployment. It is far easier to deploy an application that knows about all of its dependencies than it is to deploy one whose collection of resources must be externally defined. Some projects get around this by combining the two approaches; for instance, the application might define the resource, such as configuring a database connection pool, but then extract the JDBC URL from the deployment environment.
Database connections are probably the most common resource to configure for an enterprise application, so we'll try configuring a database connection as a way of learning more about TomEE.
The first step is to copy your JDBC driver (JAR or ZIP file) to the tomee/lib folder so that the classloader will be able to find it when TomEE starts up. Next, you must configure the database connection
in a <Resource> XML node in the tomee/conf/tomee.xml file.
To create a data source, define a <Resource> node with a type of "DataSource," as shown here:
<Resource id="MyDataSource" type="DataSource">
</Resource>
The body of this node accepts a simple set of name-value pairs that configure the data source.
Resource configuration is a huge topic, so I direct you to the TomEE homepage for complete documentation. We'll practice with a database configuration, which includes all of the options summarized below.
JtaManaged: Determines whether the data source should be JTA managed or user managed. If set to true (the default value), then the data source will automatically join any ongoing transactions. Calling begin/commit/rollback or setAutoCommit on the data source or connection will not be allowed. If you need to perform these functions yourself, set JtaManaged to false.
JdbcDriver: The class name of the JDBC driver. The default configuration is set to use Hypersonic DB so the default value is org.hsqldb.jdbcDriver.
JdbcUrl: The JDBC URL that defines how to connect to the database. The default value is jdbc:hsqldb:file:data/hsqldb/hsqldb.
UserName: The database username. The default value is sa.
Password: The password for the specified username.
ConnectionProperties: The connection properties that will be sent to the JDBC driver when establishing new connections. The format of the string
must be [propertyName=property;]*DefaultAutoCommit: Defines whether or not transactions are automatically committed. The default value is true.
DefaultReadOnly: The default state for determining whether or not connections to the database should be read-only. Some databases do not
support read-only connections, which case this value will not be passed in.
DefaultTransactionIsolation: If the transaction isolation level is not set then this is the value that it is configured to. Valid values are NONE, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, and SERIALIZABLE. The default value is database dependent.
InitialSize: The initial number of connections to create in the pool. The default value is 0.
MaxActive: The maximum number of connections that can be active in this connection pool at any given time. The default value is 20.
MaxIdle: The maximum number of connections that can be idle in the connection pool without extra connections being released. The
default is 20, but you can set a negative number to mean unlimited.
MinIdle: The minimum number of connections that can remain idle in the pool without extra ones being created. The default value is
0, which creates none.
MaxWait: The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to
be returned before throwing an exception. The default value is -1, which waits indefinitely.
ValidationQuery: The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified,
this query MUST be an SQL SELECT statement that returns at least one row.
TestOnBorrow: If true then connections will be validated before being returned from the pool. If the validation fails, the connection is destroyed,
and a new connection will be retrieved from the pool (and validated). The default value is true.
TestOnReturn: If true then connections will be validated before being returned to the pool. If the validation fails, the connection is destroyed
instead of being returned to the pool. The default value is false.
TestWhileIdle: If true then connections will be validated by the idle connection evictor (if any). If the validation fails, the connection is destroyed
and removed from the pool. The default value is false.
TimeBetweenEvictionRunsMillis: The number of milliseconds to sleep between runs of the idle connection evictor thread. When set to a negative number, no
idle connection evictor thread will be run. The default value is -1.
NumTestsPerEvictionRun: The number of connections to examine during each run of the idle connection evictor thread (if any). Default value is 3.
MinEvictableIdleTimeMillis: The minimum amount of time in milliseconds a connection may sit idle in the pool before it is eligible for eviction by the
idle connection evictor (if any). Default value is 1800000.
PoolPreparedStatements: If true then a statement pool is created for each Connection and PreparedStatements are pooled. The default value is false.
MaxOpenPreparedStatements: The maximum number of open statements that can be allocated from the statement pool at the same time, or zero for no limit.
The default value is 0.
AccessToUnderlyingConnectionAllowed: If true the raw physical connection to the database can be accessed by the application. The default value is false.
That's a long list, but if you're working with TomEE it will be important that you understand what each parameter means and the impact of its default behavior. Listing 1, an example of a database connection being created for a movie database in MySQL, demonstrates a realistic database configuration.
More about deployment servers, containers, and platforms on JavaWorld
From Apache's OpenEJB/TomEE homepage
More from JavaWorld