blob: 60129eb64294491455f0c47a87ec1094c1ebba14 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DataSource Configuration</title>
<meta name="description" content="Apache TomEE">
<meta name="author" content="Apache TomEE">
<meta name="google-translate-customization" content="f36a520c08f4c9-0a04e86a9c075ce9-g265f3196f697cf8f-10">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, max-age=0">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="./resources/css/bootstrap.css" rel="stylesheet">
<link href="./resources/css/prettify.css" rel="stylesheet">
<!--link href="./resources/css/bootstrap-mods.css" rel="stylesheet"-->
<link href="./resources/css/main.css" rel="stylesheet">
<link href="./resources/font-awesome-4.6.3/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
var t = encodeURIComponent(document.title.replace(/^\s+|\s+$/g,""));
var u = encodeURIComponent(""+document.URL);
function fbshare () {
window.open(
"http://www.facebook.com/sharer/sharer.php?u="+u,
'Share on Facebook',
'width=640,height=426');
};
function gpshare () {
window.open(
"https://plus.google.com/share?url="+u,
'Share on Google+',
'width=584,height=385');
};
function twshare () {
window.open(
"https://twitter.com/intent/tweet?url="+u+"&text="+t,
'Share on Twitter',
'width=800,height=526');
};
function pinshare () {
window.open("//www.pinterest.com/pin/create/button/?url="+u+"&media=http%3A%2F%2Ftomee.apache.org%2Fresources%2Fimages%2Ffeather-logo.png&description="+t,
'Share on Pinterest',
'width=800,height=526');
};
</script>
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./favicon.ico">
<link rel="apple-touch-icon" href="./resources/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./resources/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./resources/images/apple-touch-icon-114x114.png">
<script src="./resources/js/prettify.js" type="text/javascript"></script>
<script src="./resources/js/jquery-latest.js"></script>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<script src="./resources/js/common.js"></script>
<script src="./resources/js/prettyprint.js"></script>
<!--script src="//assets.pinterest.com/js/pinit.js" type="text/javascript" async></script//-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2717626-1']);
_gaq.push(['_setDomainName', 'apache.org']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="topbar" data-dropdown="dropdown">
<div class="fill">
<div class="container">
<a class="brand" href="./index.html">Apache TomEE</a>
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Apache
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<!-- <li><a href="./misc/whoweare.html">Who we are?</a></li> -->
<!-- <li><a href="./misc/heritage.html">Heritage</a></li> -->
<li><a href="http://www.apache.org">Apache Home</a></li>
<!-- <li><a href="./misc/resources.html">Resources</a></li> -->
<li><a href="./misc/contact.html">Contact</a></li>
<li><a href="./misc/legal.html">Legal</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li class="divider"/>
<li><a href="http://www.apache.org/security">Security</a></li>
</ul>
</li>
<li><a href="./index.html">Home</a></li>
<li><a href="./downloads.html">Downloads</a></li>
<li><a href="./documentation.html">Documentation</a></li>
<li><a href="./examples-trunk/index.html">Examples</a></li>
<li><a href="./support.html">Support</a></li>
<li><a href="./contribute.html">Contribute</a></li>
<li><a href="./security/index.html">Security</a></li>
</ul>
<!-- Google CSE Search Box Begins -->
<FORM class="pull-right" id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse">
<INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90">
<INPUT type="hidden" name="cof" value="FORID:0">
<INPUT size="18" width="130" style="width:130px" name="q" type="text" placeholder="Search">
</FORM>
<!--<SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT>-->
<!-- Google CSE Search Box Ends -->
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<small><a href="./index.html">Home</a></small><br>
<h1>DataSource Configuration
<div style="float: right; position: relative; bottom: -10px; ">
<a onclick="javascript:gpshare()" class="gp-share sprite" title="Share on Google+">share [gp]</a>
<a onclick="javascript:fbshare()" class="fb-share sprite" title="Share on Facebook">share [fb]</a>
<a onclick="javascript:twshare()" class="tw-share sprite" title="Share on Twitter">share [tw]</a>
<a onclick="javascript:pinshare()" class="pin-share sprite" title="Share on Pinterest">share [pin]</a>
<a data-toggle="modal" href="#edit" class="edit-page" title="Contribute to this Page">contribute</a>
</div>
</h1>
</div>
<p>A DataSource can be declared via xml in the <code>&lt;tomee-home&gt;/conf/tomee.xml</code> file or in a <code>WEB-INF/resources.xml</code> file using a declaration like the following. All properties in the element body are optional.</p>
<pre><code>&lt;Resource id="myDataSource" type="javax.sql.DataSource"&gt;
accessToUnderlyingConnectionAllowed = false
alternateUsernameAllowed = false
connectionProperties =
defaultAutoCommit = true
defaultReadOnly =
definition =
ignoreDefaultValues = false
initialSize = 0
jdbcDriver = org.hsqldb.jdbcDriver
jdbcUrl = jdbc:hsqldb:mem:hsqldb
jtaManaged = true
maxActive = 20
maxIdle = 20
maxOpenPreparedStatements = 0
maxWaitTime = -1 millisecond
minEvictableIdleTime = 30 minutes
minIdle = 0
numTestsPerEvictionRun = 3
password =
passwordCipher = PlainText
poolPreparedStatements = false
serviceId =
testOnBorrow = true
testOnReturn = false
testWhileIdle = false
timeBetweenEvictionRuns = -1 millisecond
userName = sa
validationQuery =
&lt;/Resource&gt;
</code></pre>
<p>Alternatively, a DataSource can be declared via properties in the <code>&lt;tomee-home&gt;/conf/system.properties</code> file or via Java VirtualMachine <code>-D</code> properties. The properties can also be used when embedding TomEE via the <code>javax.ejb.embeddable.EJBContainer</code> API or <code>InitialContext</code></p>
<pre><code>myDataSource = new://Resource?type=javax.sql.DataSource
myDataSource.accessToUnderlyingConnectionAllowed = false
myDataSource.alternateUsernameAllowed = false
myDataSource.connectionProperties =
myDataSource.defaultAutoCommit = true
myDataSource.defaultReadOnly =
myDataSource.definition =
myDataSource.ignoreDefaultValues = false
myDataSource.initialSize = 0
myDataSource.jdbcDriver = org.hsqldb.jdbcDriver
myDataSource.jdbcUrl = jdbc:hsqldb:mem:hsqldb
myDataSource.jtaManaged = true
myDataSource.maxActive = 20
myDataSource.maxIdle = 20
myDataSource.maxOpenPreparedStatements = 0
myDataSource.maxWaitTime = -1 millisecond
myDataSource.minEvictableIdleTime = 30 minutes
myDataSource.minIdle = 0
myDataSource.numTestsPerEvictionRun = 3
myDataSource.password =
myDataSource.passwordCipher = PlainText
myDataSource.poolPreparedStatements = false
myDataSource.serviceId =
myDataSource.testOnBorrow = true
myDataSource.testOnReturn = false
myDataSource.testWhileIdle = false
myDataSource.timeBetweenEvictionRuns = -1 millisecond
myDataSource.userName = sa
myDataSource.validationQuery =
</code></pre>
<p>Properties and xml can be mixed. Properties will override the xml allowing for easy configuration change without the need for ${} style variable substitution. Properties are not case sensitive. If a property is specified that is not supported by the declared DataSource a warning will be logged. If a DataSource is needed by the application and one is not declared, TomEE will create one dynamically using default settings. Multiple DataSource declarations are allowed.</p>
<p>See the <a href="common-datasource-configurations.html">Common DataSource Configurations</a> page for examples of configuring datasources for Derby, MySQL, Oracle and other common databases.</p>
<h1>Supported Properties</h1>
<table>
<tr>
<th>Property</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td><a href="#accessToUnderlyingConnectionAllowed">accessToUnderlyingConnectionAllowed</a></td>
<td>boolean</td>
<td>false</td>
<td>
If true the raw physical connection to the database can be
accessed
</td>
</tr>
</tr>
<tr>
<td><a href="#alternateUsernameAllowed">alternateUsernameAllowed</a></td>
<td>boolean</td>
<td>false</td>
<td>
If true allow an alternate username and password to be specified on the connection, rather than those specified in the DataSource definition..
</td>
</tr>
<tr>
<td><a href="#connectionProperties">connectionProperties</a></td>
<td>String</td>
<td></td>
<td>
The connection properties that will be sent to the JDBC
driver when establishing new connections
</td>
</tr>
<tr>
<td>defaultAutoCommit</td>
<td>boolean</td>
<td>true</td>
<td>
The default auto-commit state of new connections
</td>
</tr>
<tr>
<td>defaultReadOnly</td>
<td>String</td>
<td></td>
<td>
The default read-only state of new connections
If not set then the setReadOnly method will not be called.
(Some drivers don't support read only mode, ex: Informix)
</td>
</tr>
<tr>
<td>definition</td>
<td>String</td>
<td></td>
<td>
</td>
</tr>
<tr>
<td>ignoreDefaultValues</td>
<td>boolean</td>
<td>false</td>
<td>
use only all set values in this config
will need a lot of properties but allow to not set some values
</td>
</tr>
<tr>
<td><a href="#initialSize">initialSize</a></td>
<td>int</td>
<td>0</td>
<td>
The size to reach when creating the datasource.
</td>
</tr>
<tr>
<td>jdbcDriver</td>
<td>String</td>
<td>org.hsqldb.jdbcDriver</td>
<td>
Driver class name
</td>
</tr>
<tr>
<td>jdbcUrl</td>
<td>java.net.URI</td>
<td>jdbc:hsqldb:mem:hsqldb</td>
<td>
Url for creating connections
</td>
</tr>
<tr>
<td><a href="#jtaManaged">jtaManaged</a></td>
<td>boolean</td>
<td>true</td>
<td>
Determines wether or not this data source should be JTA managed
or user managed.
</td>
</tr>
<tr>
<td>maxActive</td>
<td>int</td>
<td>20</td>
<td>
The maximum number of active connections that can be
allocated from this pool at the same time, or a negative
number for no limit. N.B. When using dbcp2 with TomEE 7 ("DataSourceCreator dbcp"), "MaxTotal" should be used as opposed to "MaxActive".
</td>
</tr>
<tr>
<td>maxIdle</td>
<td>int</td>
<td>20</td>
<td>
The maximum number of connections that can remain idle in
the pool, without extra ones being released, or a negative
number for no limit.
</td>
</tr>
<tr>
<td><a href="#maxOpenPreparedStatements">maxOpenPreparedStatements</a></td>
<td>int</td>
<td>0</td>
<td>
The maximum number of open statements that can be allocated
from the statement pool at the same time, or zero for no
limit.
</td>
</tr>
<tr>
<td>maxWaitTime</td>
<td><a href="configuring-durations.html">time</a></td>
<td>-1&nbsp;millisecond</td>
<td>
The maximum number of time that the pool will wait
(when there are no available connections) for a connection
to be returned before throwing an exception, or -1 to wait
indefinitely.
</td>
</tr>
<tr>
<td>minEvictableIdleTime</td>
<td><a href="configuring-durations.html">time</a></td>
<td>30&nbsp;minutes</td>
<td>
The minimum amount of time a connection may sit idle in the
pool before it is eligable for eviction by the idle
connection evictor (if any).
</td>
</tr>
<tr>
<td>minIdle</td>
<td>int</td>
<td>0</td>
<td>
The minimum number of connections that can remain idle in
the pool, without extra ones being created, or zero to
create none.
</td>
</tr>
<tr>
<td>numTestsPerEvictionRun</td>
<td>int</td>
<td>3</td>
<td>
The number of connectionss to examine during each run of the
idle connection evictor thread (if any).
</td>
</tr>
<tr>
<td>password</td>
<td>String</td>
<td></td>
<td>
Default password
</td>
</tr>
<tr>
<td>passwordCipher</td>
<td>String</td>
<td>PlainText</td>
<td>
</td>
</tr>
<tr>
<td><a href="#poolPreparedStatements">poolPreparedStatements</a></td>
<td>boolean</td>
<td>false</td>
<td>
If true, a statement pool is created for each Connection and
PreparedStatements created by one of the following methods are
pooled:
</td>
</tr>
<tr>
<td>serviceId</td>
<td>String</td>
<td></td>
<td>
</td>
</tr>
<tr>
<td><a href="#testOnBorrow">testOnBorrow</a></td>
<td>boolean</td>
<td>true</td>
<td>
If true connections will be validated before being returned
from the pool. If the validation fails, the connection is
destroyed, and a new conection will be retrieved from the
pool (and validated).
</td>
</tr>
<tr>
<td><a href="#testOnReturn">testOnReturn</a></td>
<td>boolean</td>
<td>false</td>
<td>
If true 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.
</td>
</tr>
<tr>
<td><a href="#testWhileIdle">testWhileIdle</a></td>
<td>boolean</td>
<td>false</td>
<td>
If true connections will be validated by the idle connection
evictor (if any). If the validation fails, the connection is
destroyed and removed from the pool
</td>
</tr>
<tr>
<td>timeBetweenEvictionRuns</td>
<td><a href="configuring-durations.html">time</a></td>
<td>-1&nbsp;millisecond</td>
<td>
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.
</td>
</tr>
<tr>
<td>userName</td>
<td>String</td>
<td>sa</td>
<td>
Default user name
</td>
</tr>
<tr>
<td>validationQuery</td>
<td>String</td>
<td></td>
<td>
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.
</td>
</tr>
<tr>
<td>LogSql</td>
<td>boolean</td>
<td>false</td>
<td>Wether SQL queries should be logged or not</td>
</tr>
</table>
<p><a name="accessToUnderlyingConnectionAllowed"></a></p>
<h2>accessToUnderlyingConnectionAllowed</h2>
<p>If true the raw physical connection to the database can be
accessed using the following construct:</p>
<pre><code>Connection conn = ds.getConnection();
Connection rawConn = ((DelegatingConnection) conn).getInnermostDelegate();
...
conn.close()
</code></pre>
<p>Default is false, because misbehaving programs can do harmfull
things to the raw connection shuch as closing the raw
connection or continuing to use the raw connection after it
has been assigned to another logical connection. Be careful
and only use when you need direct access to driver specific
extensions.</p>
<p>NOTE: Do NOT close the underlying connection, only the
original logical connection wrapper.</p>
<p><a name="connectionProperties"></a></p>
<h2>connectionProperties</h2>
<p>The connection properties that will be sent to the JDBC
driver when establishing new connections</p>
<p>Format of the string must be [propertyName=property;]*</p>
<p>NOTE - The "user" and "password" properties will be passed
explicitly, so they do not need to be included here.</p>
<p><a name="TransactionIsolation"></a></p>
<h2>TransactionIsolation</h2>
<p>The default TransactionIsolation state of new connections.</p>
<p>If not set then the <code>setTransactionIsolation</code> method will not
be called. The allowed values for this property are:</p>
<ul>
<li><code>NONE</code></li>
<li><code>READ_COMMITTED</code></li>
<li><code>READ_UNCOMMITTED</code></li>
<li><code>REPEATABLE_READ</code></li>
<li><code>SERIALIZABLE</code></li>
</ul>
<p>Note: Most JDBC drivers do not support all isolation levels
DefaultTransactionIsolation</p>
<p><a name="initialSize"></a></p>
<h2>initialSize</h2>
<p>The initial size to initialize the pool of connections.</p>
<p><a name="jtaManaged"></a></p>
<h2>jtaManaged</h2>
<p>Determines wether or not this data source should be JTA managed
or user managed.</p>
<p>If set to 'true' it will automatically be enrolled
in any ongoing transactions. Calling begin/commit/rollback or setAutoCommit
on the datasource or connection will not be allowed. If you need to perform
these functions yourself, set <code>JtaManaged</code> to <code>false</code></p>
<p>In terms of JPA persistence.xml:</p>
<ul>
<li><code>JtaManaged=true</code> can be used as a 'jta-data-source'</li>
<li><code>JtaManaged=false</code> can be used as a 'non-jta-data-source'</li>
</ul>
<p><a name="maxOpenPreparedStatements"></a></p>
<h2>maxOpenPreparedStatements</h2>
<p>The maximum number of open statements that can be allocated
from the statement pool at the same time, or zero for no
limit.</p>
<p>NOTE - Some drivers have limits on the number of open
statements, so make sure there are some resources left
for the other (non-prepared) statements.</p>
<p><a name="poolPreparedStatements"></a></p>
<h2>poolPreparedStatements</h2>
<p>If true, a statement pool is created for each Connection and
PreparedStatements created by one of the following methods are
pooled:</p>
<pre><code>public PreparedStatement prepareStatement(String sql);
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
</code></pre>
<p><a name="testOnBorrow"></a></p>
<h2>testOnBorrow</h2>
<p>If true connections will be validated before being returned
from the pool. If the validation fails, the connection is
destroyed, and a new conection will be retrieved from the
pool (and validated).</p>
<p>NOTE - for a true value to have any effect, the
ValidationQuery parameter must be set.</p>
<p><a name="testOnReturn"></a></p>
<h2>testOnReturn</h2>
<p>If true 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.</p>
<p>NOTE - for a true value to have any effect, the
ValidationQuery parameter must be set.</p>
<p><a name="testWhileIdle"></a></p>
<h2>testWhileIdle</h2>
<p>If true connections will be validated by the idle connection
evictor (if any). If the validation fails, the connection is
destroyed and removed from the pool</p>
<p>NOTE - for a true value to have any effect, the
timeBetweenEvictionRunsMillis property must be a positive
number and the ValidationQuery parameter must be set.</p>
<h1>XADataSource</h1>
<p>There are several ways to configure a XADataSource. Depending the underlying datasource (Oracle, MySQL one or the other
solution can be more adapted.</p>
<p>This part deals with <code>JtaManaged</code> XaDataSource since a not managed XaDataSource can be defined as a standard
resource using <code>class-name</code>.</p>
<h2>Single definition</h2>
<p>First solution is to define as <code>JdbcDriver</code> an XADataSource:</p>
<pre><code>&lt;Resource id="myXaDs" type="DataSource"&gt;
JdbcDriver = org.foo.MyXaDataSource
myXaProperty = value
myPoolProperty = 10
&lt;/Resource&gt;
</code></pre>
<p>This solution merges properties for the XaDataSource and the pool (tomcat-jdbc for TomEE, dbcp for OpenEJB by default
but still configurable with DataSourceCreator).</p>
<p>Note: in this case for Oracle for instance you'll define UserName for the pool and User for the datasource which
can look weird if you don't know properties are used for 2 instances (pool and datasource).</p>
<p>Note: this solution uses the same logic than @DataSourceDefinition factory mecanism.</p>
<h2>Two resources definition</h2>
<p>An alternative is to define a resource for the XaDataSource:</p>
<pre><code>&lt;Resource id="myXa" class-name="org.foo.MyXaDataSource"&gt;
myXaProperty = value
&lt;/Resource&gt;
</code></pre>
<p>And then wrap it in the pool:</p>
<pre><code>&lt;Resource id="myXaDs" type="DataSource"&gt;
DataSourceCreator = [dbcp|dbcp-alternative]
myPoolProperty = 10
&lt;/Resource&gt;
</code></pre>
<p>Note: <code>dbcp</code> is more adapted than <code>dbcp-alternative</code> in most of the case because it is reusing direct dbcp JTA management.</p>
<h2>Known issues</h2>
<p>For TomEE 1.7.0/1.7.1 you can need to add the property:</p>
<pre><code> openejb.datasource.pool = true
</code></pre>
<p>in resource properties to ensure the resource is pooled.</p>
<h2>Details about DataSource and their factories (advanced configuration)</h2>
<p><a href="datasource-configuration-by-creator.html">Configuration by creator</a></p>
<div id="edit" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Thank you for contributing to the documentation!</h3>
</div>
<div class="modal-body">
<h4>Any help with the documentation is greatly appreciated.</h4>
<p>All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)</p>
<small>NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.</small>
<!--[if gt IE 6]>
<h4>Internet Explorer Users</h4>
<p>If you are not an Apache committer, click the Yes link and enter a <i>anonymous</i> for the username and leave the password empty</p>
<![endif]-->
</div>
<div class="modal-footer">
Do you have an Apache ID?
<a href="javascript:void(location.href='https://cms.apache.org/redirect?uri='+escape(location.href))" class="btn">Yes</a>
<a href="javascript:void(location.href='https://anonymous:@cms.apache.org/redirect?uri='+escape(location.href))" class="btn">No</a>
</div>
</div>
<script src="./resources/js/bootstrap-modal.js"></script>
<footer>
<p>Copyright &copy; 1999-2016 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.
Apache TomEE, TomEE, Apache, the Apache feather logo, and the Apache TomEE project logo are trademarks of The Apache Software Foundation.
All other marks mentioned may be trademarks or registered trademarks of their respective owners.</p>
</footer>
</div> <!-- /container -->
<!-- Javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./resources/js/bootstrap-dropdown.js"></script>
</body>
</html>