Can I change the 'appName' on an already open JTDS connection? - java

I'm looking for a way to pass web-application transaction information on to the underlying database process. In my Java code I might have a transactional method ReservationService#search(), which runs one or several SQLs. On the DBMS I just see a SPID along with some locks. I'm looking for a way to add a tag "ReservationService#search" to the database process.
jTDS / Sybase ASE have an appName which can be passed in as a connection property. As we're using a connection pool, existing connections are re-used, but to my knowledge the appName is only read on establishing a new connection.
How can I re-set the appName on an already existing connection (without closing/opening)? Or, if that simply is impossible, are there any other ideas to get transactional context information from Java to the DBMS?
Tomcat Webapplication (Java 6)
C3P0 Connection Pool (only supports JDBC 3)
jTDS connecting to Sybase ASE 15
Thanks
Simon

Unfortunately not, it seems that you can only specify that in the URL parameters when you open up the connection but can't be altered afterwords.
You can aways pass in a SessionID of some kind from your Java/Tomecat to all your Sybase queries. For me, this was easy as I use stored procedures for all communications between my Java application and the SQL server. I based my SessionID in Java on the J2EE session.

Related

What does JDBC Connection really mean? Wanted to know internals of the same

Is "opening the JDBC Connection" related to open a Socket with the DataBase software?
Databases provides connections to client eg. a GUI Administrative tool to manage related database like SQLyog for MySQL and so in JDBC connection we are requesting a connection from our application as client through JDBC API.
These connection as TCP connections so a client is required to know the port no and ip of the database server to talk to it.
what you must be wondering at is what is JDBC? if so then
JDBC is an interface provided by java that manages database operations and most awesome thing about this interface you will be writing exactly same code whether you are querying a MySQL or a PostgreSQL database servers.
It depends on what type of JDBC driver you are using. According to
http://en.wikipedia.org/wiki/Java_Database_Connectivity
you may have four types of JDBC drivers which usually (but not necessarily always) rely on TCP/Socket connections.
A JDBC connection can actually be anything - it's a generic way to talk to databases.
In most cases it is a TCP/IP connection but that is not always the case. For example you can embed a Derby Databse within your Java application and talk directly to that without even leaving your process.
If you find out what your database and driver is that should allow you to answer your question.
Here is what you require.This should answer all your queries. Cheers.

What exactly is a data source? What difference does it make?

When I tried to establish a connection with the Oracle Database, I had to write
Connection CON = DriverManager.getConnection("jdbc:odbc:Dan", "system", "noodles");
Here, Dan is the data source name, isn't it? What if I created a table called cBC when the data source was Dan and what if I rename the data source and enter further rows into the table? What difference does it make?
Dan is the name of a ODBC connection configured on your machine. The name itself does not matter, as long as the database that it is configured to connect to is the same, it doesn't matter if you call it Dan, MyDatabase or foobar.
Note that this specific way to access a database that is configured externally is not a thing that JDBC does in general, it's a specific behaviour of the JDBC-ODBC bridge (which lets you access ODBC connections via JDBC).
Other JDBC drivers (such as MySQL) use a different syntax where the necessary configuration for accessing the database is encoded in the URL: jdbc:mysql://myDbServer/myDbName.
Note also that the JDBC-ODBC bridge was never intended for production-quality DB connections (it will even be removed in Java 8!). It is just a quick way to use an existing setup.
For Oracle DB connections you should instead use the appropriate Type 4 driver from Oracle. Those drivers use an URL in the form jdbc:oracle:thin:#//<host>:<port>/ServiceName (generally speaking, the part after jdbc: identifies the JDBC driver to be used).

How to Create A Connection Pool/Data Source rather than connecting to A SQL DB

I have code that connects to a SQL DB and queries it based on user input. I would like to connect using a Pool instead to speed up the query times. I attempted to write a Pool and Manager class but I am getting an unreported NamingException when I try to get a connection from the pool. I have also already caught NamingExceptions in my getConnection() function.
Does anyone know why I am getting this error?
Or could point me in the right direction to create a valid ConnectionPool?
You should use the latest JDBC driver, which supports connection pooling internal, so you don't have to write your own.
You have 3 options:
If your code runs in an application server, configure a connection pool in the server.
Some JDBC drivers provide a connection pool implementation (but many only provide a poolable data source that is meant for integration with an app server).
Use commons-dbcp or some equivalent library to create a connection pool.

JDBC Connection Link Failure - How to fail over?

I have a stand-alone Java windows application developed based on Swing. It connects to a MySQL database for data storage. In case the database connection fails, I am getting a link failure exception from the MySQL JDBC driver (MySQLNonTransientConnectionException). I don't want to re-instantiate my database connection object or the whole program in case such a link failure issue happens. I just want to tell the user to try again later without having to restart the entire application. If the user is asked to restart the entire application, that would probably give a negative impression on the quality of the program. What do you think would be the preferred way for a standard java application to fail-over after such a database link failure without having to re-instantiate all the communication objects? Thanks in advance.
Use a Connection Pool (such as C3PO or DBCP). Your application takes the Connections from the pool, executes the statement(s) and puts the Connection back into the pool. The pool can be configured to test the JDBC Connections. For example, if they become stale, they can be automatically reinstantiated by the pool.
If your application takes the Connection from the pool, it will be a valid Connection. Let the pool handle the management of valid/invalid/stale JDBC Connections.

JavaDB connection issues; database not found

I am having a problem with Java DB that I just don't know how to resolve. I am creating a DB and connecting to it using Java DB's native JDBC driver. If I relocate that database physically and try to connect to it using its new path, I consistently get XJ004 errors:
ERROR XJ004: Database 'blahblah' not found.
I am sure I am using the correct connection string. Is there any possibility the DB is somehow getting corrupted? Or is there some encoding of the DB path in the DB such that if you relocate a Java DB it gets confused?
I'm really at a loss here. :( Please help!
Jim
Have you verified that this error message isn't also used when there's no listener on the host machine ... and were you using JavaDB on your local machine before the relocation? Many database systems (and I'm not that familiar with JavaDB) ship set-up to only allow connections from localhost for security reasons. On PostgreSQL for instance, you have to allow TCP connections and bounce the daemon to obtain a remote connection.
Anyway ... since the problem started when you when remote, look for issues related to that first! (And if you can run your application on the remote machine, does that work?)
There must be a file named derby.log somewhere. Check the error there. If it is not detailed enough, try setting derby.stream.error.logSeverityLevel to a lower value. See the manual for more information.

Categories