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 </Resource>
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:
@Resource(name="TestDS") 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.