-
Resource Injection
I've recently discovered that instead of using
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://localhost/DBname", "username", "password");
to connect to a database, you can use @Resource annotation to inject into a DataSource which will pool the connection rather than repeatedly open and close like the above, but the example I saw in a book only gave an example of:
@Resource(name="jdbc/derby")
private DataSource dataSource;
and nothing else.
My question is, how would I modify the first piece of code to work with the above @Resource style? I've tried playing around with it, but to no avail.
-
Re: Resource Injection
Been a while since I've used those sorts of annotations, but from memory I believe you need to specify the data source through some sort of xml file read by the container. Again, been a while so take that with a grain of salt. An alternative is to just create a manager class which encapsulates the connections - thus you need those two lines of code in one place in the project rather that needing to reuse them continually
-
Re: Resource Injection
Yeah you were right, needed to create a persistence.xml file, which was easy enough to create via IDE wizard. Afterwards, all that was needed was to specify my database name in the annotation parameters.