Web Services :: Share JDBC Datasource Connection Among All Servlets
Apr 20, 2015
I am new to java. I have recently learned JDBC connection pool in tomcat. To make code reuse I want to share the connection among all servlets without any conflict.
Here My code snippet:
public class GetConnection{
private DataSource ds;
public Connection getConnection(){
try {
InitialContext initialContext = new InitialContext();
Context context = (Context) initialContext.lookup("java:comp/env");
ds = (DataSource) context.lookup("connpool");
[Code] .....
Is this right way to do. Or I will get any problem due to concurrent threads.
I configured a connection pool in tomcat 7. For every database activity I get a connection from the pool. Do I have to close the connection like other regular connection after the database operation is done? If I close the connection will it have any effect on the connection pool?
I am trying to get RJDBC to work so I can connect to Oracle from R doing some data analysis..
This connection string works for my local db which I have it running in my Virtual Box
con <- dbConnect(drv, "jdbc:oracle:thin:@//localhost:1521/orcl", "demo", "demo")
However, it does not work when I do it for the remote db that I really need to pull data from...
> con <- dbConnect(drv, "jdbc:oracle:thin:@//ymsdbppr-scan:1522/YMQCTPRD", "user_read", "user_read")
Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], : java.sql.SQLException: Listener refused the connection with the following error: ORA-12514, TNS: listener does not currently know of service requested in connect descriptor
I have installed and configured Oracle Business Intelligence Applications on top of one demo eBS instance as a source. Now I would like to change the OLTP to a different eBS instance but am not sure which steps to take. I don't know if I can add new EBS connection pool and a new DataWarehouse Connection Pool in the administration and have old ones preserved or I need to overwrite the old ones? Or (ideally) can I just change the OLTP and overwrite the old DWH?
Any document supporting the process of changing the eBS OLTP database without new installation of the OBIAPPS?
Suppose I issue a command such as "ALTER SESSION SET WORKAREA_SIZE_POLICY=MANUAL" inside a stored procedure called via JDBC and then release the connection back to the pool. When the connection is next reused by a completely independant Weblogic thread is there any chance that the non-default setting could be inherited by the new thread?
I realize that there is a conceptual difference between a process, a connection, and a session so theoretically the answer to my question should be "no."
Connection from Java app (installed on desktop) to sybase database (deployed on AIX machine) is going unencrypted.i.e Traffic can be read by some hacking tool and to fix I need to enable SSL/TLS.I am using JDBC to connect to sybase. I believe apart from setting the jconnect properties there are several other things which needs to be done.
Yesterday, I established a connection with oracle 12c, codes is here:
ods.setURL("jdbc:oracle:thin:c##mytest/myt@//myhost:1521/orcl");Connection conn=ods.getConnection(); where "c##mytest/myt" is username and password. It worked well.
But, today, "IO Error:The Network Adapter could not establish the connection" appeared when I tried to run my program again.
I changed //myhost into 127.0.0.1, it worked again.(Before I did this, I checked lsnrctl and regedit and firewall....., no difference)
Here is my question:
1) What happens between //myhost to 127.0.0.1, hostname cannot be use to JDBC? If yes, how?
Besides, I have some other questions:
2) How import a *.dmp file which exported from 10g into 12c?
In the class that actually uses it I create a method variable for the connection object.
Closing the connection as well as pds.getConnection().close();e
I am thinking that at the most I should have only one inactive connection showing up when I monitor the session. How do I configure the pool as to only show on inactive connection? I am running the test queries once every five minutes. But I am opening three connections each time.
I have small Java code, which execute every day and checks for data in database using Cronj Schedular and everything works fine, but recently I have observed that, it is failing due to
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.110.xx.xxx:1521/test
At the same time, when I run my test code to check Database connectivity that works fine without above exception. I'm unable to figure it out. Although, there was just slight code change, but that was nowhere related to Database or Database connection.
dbconf.java public class dbconf { private Connection connect; private String connstr; public Connection getConnection() throws SQLException { connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";
[Code] ....
Application Log file
Wed Jul 01 09:25:17 IST 2015:------- Initializing ------------------- Wed Jul 01 09:25:17 IST 2015:------- Scheduling Jobs ---------------- Wed Jul 01 09:25:17 IST 2015:------- Job Started Running ---------------- Thu Jul 02 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2 java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test Sat Jul 04 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2 Sun Jul 05 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2 java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test
So, you can see, It failed on 3rd of July and 5th July as well. But, in between it ran fine.
I am trying to make a simple login using netbeans, derby database included in netbeans and tomcat server. I made everything nice and separated: I have a model package with a class called DbConnector that has the following method:
Then i have another class, an userDAODB that has a password check method:
public boolean checkPassword(String user, String password) { try (Connection con = new DbConnector().connect(); Statement stmt = con.createStatement()) { //checks the password in the database
In the main method of this class i tested everything, it works very nice, logs me in, other methods work too, no problem what so ever.But then i go to my servlet:
public class LoginTest extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String username = request.getParameter("username"); String pass = request.getParameter("pass"); UserDAODB userDAO = new UserDAODB(); boolean authe = userDAO.checkPassword(username, pass);
[code]....
So when i start the webpage and i try to click on the login button I get a NullPointerException com.model. UserDAODB. check Password (User DAODB. java:14) - so line 2 here
I have been googling a bit, i placed the derby.jar and the derbyclient.jar in the lib directory of tomcat, i tried to modify the context.xml of my application, but then it wouldn't even start anymore.
I am setting up a standalone JNDI and loading a Datasource to the JNDI. DataSource I use is:
org.apache.commons.dbcp.BasicDataSource
The JNDI is set up as follows
String detectorHost = InetAddress.getLocalHost().getHostName(); System.out.println("detectorHost: " + detectorHost); System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); final NamingBeanImpl namingInfo = new NamingBeanImpl(); namingInfo.start();
[Code] ....
I get the following exception
javax.naming.CommunicationException [Root exception is java.io.NotSerializableException: org.apache.commons.dbcp.BasicDataSource] at org.jnp.interfaces.NamingContext.bind(NamingContext.java:677) at org.jnp.interfaces.NamingContext.bind(NamingContext.java:611) at javax.naming.InitialContext.bind(Unknown Source) at com.lombardrisk.reform.integration.ReformIntegration.createJNDIServer(ReformIntegration.java:93)
[Code] ....
I don't quite follow why I am getting a NotSerializableException exception, this is a local JNDI in the same JVM and not a remote JNDI. Not sure why this occurs.
Now, I have got a comment saying I should get the database connection from connection pool. And one more issue is that I have used the function System.getProperty("user.name") to get the username which i have logged in. But this code will be run on Unix which will not support this function. Any function where I can get the windows username. There is a function getUserPrincipal(), but this function returns a NULL. How to resolve this.
I need to inject a DataSource, but the catch is the JNDI isn't known until Runtime. I was hoping the EJB could get a System Property of the JNDI name and use that to Inject the DataSource.
And I could either @Inject or on postContstruct() set the myRuntimeJNDI variable to the JNDI name that is configured on the App Server via System Properties or some other mechanism. But the JNDI may be different depending on what environment the application is deployed to.
Is this possible? I would like to have the same EAR deployed in all our environments and not have to create a different version of the application for each environment.
I can understand basic concepts such as OOP, Threads, Events and GUI, I've coded a little but I've always had this bothering me:
Explaining by example:
public class X{ int x; String y; public static void main(String[] args){ x = 10; y = "hello ";
[Code] ....
Okay, i'm pretty certain that code won't work, but I just want to show you conceptually, not actually care whether the code works or not. In case I wanted to get that code to work I should have probably used a Swing application to get a KeyListener in the first place, but I guess I know that, and if this was working, what would happen theoretically is, the code would run, initialize x to 10, and y to hello, then when a key is pressed, it will update x to 11 and y to hello world, I'm pretty sure that's what happens.
In this case, I used a inner class to update it's parent's members, I've seen this done before and I can vouch for the fact that it's a legitimate way to code a class.
Now in this example:
class X{ int x; String y; public int getX(){ return x; } public String getY(){ return y; }
[Code] ....
So in this overly complicated example, i'm trying to share class X with both class Y and class Z, just that how to share the members of class X with the different classes without making a new instance of X. In the previous example, I could access the parent's members because the inner class was implicitly capable of accessing the parent's members. However in this case, If "Y" starts an instance of X, then how do I access it? because it's a side by side class not a hierarchy for me to access the parent's members.
calling a parameterized stored procedure in java jdbc from sql server.The stored procedure goes like this in sql
create proc patientreg @id int as begin select [patient_id],[Psurname], [pFirstname], [pMiddlename], [reg_date], [DOB], [Sex], [Phone_num], [Addr],[Email],[dbo].[fncomputeage](DOB) from [dbo].[Patient_registration] where [patient_id] = @id end please note dbo.fncompute(DOB) is a function
I'm trying to share an hashmap(or any other stuff to share data) between a java class and a servlet. The hashmap has to be filled into the servlet with some data and then I have to get those data from the java class.
What is the best approach? I cannot use a DTO because I have several concurrent requests to my servlet.
Here is my code. Obviously this doesn't do a lot but that's only because I abstracted out the problem so there are not many lines to decipher.
Java Code:
import java.util.Scanner; class threadOne extends threadTwo { public static void main(String[] args) { threadTwo threadTwoObj = new threadTwo(); threadTwoObj.start(); while (!userInput.equals("exit")) {
[Code] ....
What its supposed to do: When the user returns "exit" in the console it is supposed to break out of both while loops in both threads.
What it actually does: breaks out of the while loop in threadOne and not in threadTwo.
Basically the idea is to have a thread running doing computation and another thread able to query it for updates or interact to make changes to the flow. This will be useful, among other ways, for the sorts of problems where finding a solution is easy but where a better solution can always be found with more time. So for example finding directions on a map. Its easy to find a solution, but if you search longer you can find a faster route, if you search longer still than faster still.
I was wondering if I could take the following code and somehow put the connection to database in it's own class without the "public static void main(String[] args)" method. That way I could call it anytime i want to open a connection to the database.
Java Code:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Sample { public static void main(String[] args) throws ClassNotFoundException
I am new to EJB; I am writing EJB (3.1) program in JBoss 7.1 - I am using MySQL DB with JPA. While executing the program, I am facing the Generic JDBCException: Could not open connection. It is happening only for certain tables in the database; but NOT for all tables. My code is able to access few of the tables in the same database. Here is my full stack trace: -
Exception in thread "main" javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230) at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
I'm using JSF framework. I can let Netbeans to create the persistence unit and hardcodes connection parameters into the persistence.xml file or i can use resource tag in context.xml file or using another file like property file or class for holding connection parameters like username and password; what is the most secure way or expert way to use connection properties in my app or there is no difference?