I've tried researching how to use the DataSource method of connecting to a database but never could find out how. I know that a DataSource is first configured and registered to JNDI in an application that is separate from the user application, and all the user application will do is retrieve it using JNDI. What I don't understand is where the DataSource is configured. Is it automatically registered when I turn on MySQL, do I need to download another application to register it, or do I make a new class that will do that for me?
You usually have a Java EE app server like Glassfish, WebLogic, JBOSS, Tomcat, or Jetty have a JNDI provider that you should be using for the lookup.
Here's how you do it with Oracle.
Here's how you do it with MySQL.
The JDK 6 javadocs say that a basic DataSource can supply a connection if your driver has such an implementation. I would recommend looking at the Connector-J docs to see if you can do it without JNDI lookup services.
Related
I built a simple Java web application. It provides a series of RESTful APIs for the user to carry out certain operations on a Java DB through a web interface. I used NetBeans environment during the development, and Glassfish for testing.
Now that I finished it, I would like to be able to deploy it on another machine using binaries (although as for now I use the same machine until I learn how to do it).
I installed Tomcat 7, and moved the .war file into Tomcat's webapp folder. The application deploys. Thereafter I try to read some data from the databse using a button I created just for this, but get the following error
I am not sure what went wrong, but I have two theories.
1) The web application cannot connect to the database. Yet when I attempted to run the application again, after starting JavaDB from NetBeans, there was no difference.
2) Somehow, the application cannot reach the Node service. I assumed that there will be no need to change the API links while moving the app, but perhaps I was wrong.
Or maybe there is some other issue I did not consider? I will be grateful for any advice about how to properly deploy such an application.
EDIT: The issue was solved by using TomEE.
The error is come from your application server of choice.
TomCat is only a servlet container (means it only support Servlet/JSP).
Any other feature (JAX-RS, CDI etc) require a Java EE certified server e.g. GlassFish, WildFly,Payara, WebLogic, OpenLiberty or TomEE.
TomEE could be your best bet if you want to use TomCat in your production or test environment, it is basically TomCat + Java EE other feature.
EDIT:
TomEE don't have a GUI for JNDI datasource configuration like GlassFish, you need to edit conf/tomee.xml
<Resource id="myDataSource" type="javax.sql.DataSource">
jdbcDriver = org.apache.derby.jdbc.ClientDriver
jdbcUrl = jdbc:derby://localhost:1527/dbname
userName = app
password = app
</Resource>
And in your java code:
#Path("resources")
#Stateless
public class MyResources{
#Resource(name="myDataSource")
DataSource dataSource;
#GET
public Response SomeMethod(){
//Do stuff here
}
}
You can check here for more detail configuration on data source.
How do I change weblogic datasource at runtime?
For example.
currently it is referring to UAT database Now I want to point to Dev
database. I know it can be modified in console. I don't want to use
that method. it requires too much user interaction.
Any other easy method.
Can I modify the datasource in my app config file persistence.xml file to chg form UATdb to DevDb.
Does it require restarting my weblogic web server?
How to manage without restarting web server
Or should I have another web server instance? for UATdb and another for Devdb
You cannot modify datasources in your web.xml. JDBC is a weblogic feature. That being said, you can change the datasource info using WLST.
An example of the process is mentioned here.
https://docs.oracle.com/cd/E17904_01/web.1111/e13737/config.htm#JDBCA134
You can get creative with this process and create and destroy JDBC's based on environment as well.
So long answer short, WLST is the solution.
Update data account password
connect('weblogic', 'password', 't3://%s:%s' % ('localhost','7001'))
edit()
startEdit()
cd('/')
cd('JDBCSystemResources/dbname/JDBCResource/dbname/JDBCDriverParams/dbname/Properties/dbname/Properties/user')
cmo.setValue('ESB')
cd('/')
cd('JDBCSystemResources/dbname/JDBCResource/dbname/JDBCDriverParams/dbname')
cmo.setPassword('ESB')
cd('/')
save()
activate(block='true')
disconnect()
exit()
I have a Play Framework 2.2.2 application that I am deploying as a .war file and running under Tomcat 7. My application runs for days without problems on my local dev machine (through Play's built in server, not Tomcat), but once I deploy it under Tomcat, after several hours the Tomcat server will lock up, taking down all the other applications running on it as well.
I think the problem is that the BoneCP connection pool in Play, and the built-in connection pool of Tomcat are conflicting. There isn't much or any useful information in the Tomcat logs, so I'm kind of left guessing here.
I'd like to disable the BoneCP connection pooling within my Play application, but cannot find any information on how to do so.
Any advice appreciated!
There are several possible solutions for this, which might be more or less preferrable for your deployment environment.
Play gives you an "out-of-the-box" database connection, which you don't need to use. Drop the Play JDBC component from your build file (remove jdbc from your libraryDependencies) and setup your JDBC connections manually by yourself. For example, you can make a singleton TomcatConnectionPool that has a function getConnection() that gives you the JDBC connection you need for use in your Play actions.
Write your own plugin specifically extending Play's DBPlugin interface so that it's a database plugin. Implement it like Play's BoneCPPlugin but make it use the Tomcat connection pool instead of BoneCP.
Use someone else's already made custom Play Database Plugin, like this one that uses c3p0. I have some anecdotal evidence that c3p0 works well with Tomcat, but your mileage my vary.
In a java application, If we want to use javax.sql.DataSource instead of java.sql.DriverManager to get connections, which approach would be better to create DataSource and WHY?
Create DataSource in the application itself at the application startup time
or
Configure DataSource in the application server and get it via JNDI
Well, it actually depends upon the requirement. You can take this as a general rule.If you have multiple applications and sharing the same DataSource, then it can be a good choice to configure the DataSource in the server. Otherwise, if this is the only application uses the DataSource, then you can have it in the application level itself.
I have a JAVA webapp which is using DB connection pooling for Tomcat+MySQL config.
I have another JAVA webapp which i want to deploy in the same Tomcat and connect to same MySQL database (even access the data from same tables).
I havent figured out a way how to achieve the same.
Should I have connection pooling context.xml for each of the webapps?
Or should I have a global configuration.
In the first case , I assume there is nothing different that i need to do. Only to deploy the webapp which has its own context.xml.. Please correct me if i'm wrong.
If having a global config is a better solution, how to achieve that. Haven't found any good tutorials about it. What changes in each of webapps need to made , so that it knows that it needs to read the global config.
There's nothing wrong with having a separate context for each webapp unless you want to centrally manage changes to the database (i.e. migrate it to a different DB, change connection parameters). If you think your connection properties will change or you want that flexibility then you can use a JNDI datasource in tomcat and manage it there (google is your friend for that).