Configuring a MySQL Data Source in TomEE

Today, I hit a major brick wall trying to define a data source in TomEE. The official documentation is a little outdated. I know that the developers at TomEE do a great job and do not always get the time to updating the documentation. I thought, I will post this tutorial to help them out.

Define the Data Source

Open <TOMEE>/conf/tomee.xml. Right below the root element <tomee> add:

<Resource id="TestDS" type="DataSource">
    JdbcDriver  com.mysql.jdbc.Driver
    JdbcUrl jdbc:mysql://localhost/test
    UserName    root
    Password    some_pass

Of course, settings for your database will be different.

The id attribute defines the resource name. So, to use the data source from code, you need to inject it like this:

DataSource ds;

The JNDI name will be java:openejb/Resource/TestDS. For example, this will setup a persistence.xml.


Copy the Driver JAR File

Copy the JDBC driver JAR file (mysql-connector-java-5.1.22-bin.jar) to <TOMEE>/lib folder.

Fix Eclipse Server

At this point, our data source is fully defined. But, if you start TomEE from Eclipse, it will not use the settings in tomee.xml. That is because, by default, it uses a different configuration folder. To fix the problem, copy the <TOMEE>/conf/tomee.xml file and paste it inside the Servers/<TOMEE> folder of your workspace.


Restart the server.


6 thoughts on “Configuring a MySQL Data Source in TomEE

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.