Glassfish JDBC lookup jindi - jdbc

I created a "JDBC Connection Pools" and a "JDBC Resources" called myDB (JINDI name).
How to access this in my webapp?
I tried:
<ejb-ref>
<ejb-ref-name>myDB</ejb-ref-name>
<lookup-name>myDB</lookup-name>
</ejb-ref>
And how to access it in the code?
I tried:
InitialContext ctx = new InitialContext();
com.sun.appserv.jdbc.DataSource ds = (com.sun.appserv.jdbc.DataSource) ctx.lookup("jdbc/myDB");
Connection con = ds.getConnection();
Connection drivercon = ds.getConnection(con); //get physical connection from wrapper
Statement stmt=null;
stmt = con.createStatement();
stmt.executeUpdate("Update");
// Do db operations.
// Do not close driver connection.
stmt.close();
con.close(); // return wrapped connection to pool.
Or like this?
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myDB?"
+ "user=admin&password=admin");
I am irritated. I just want to access my relational db to select and insert things.

Related

Using JDBC Pool in oracle weblogic Server,

I have a question that puzzles me these days. I am using JDBC connection pool in oracle weblogc server for my REST API calls. The package was deployed and was able to handle the incoming requests correctly.
But somehow, after a new request is made, in the db session level, I will get a new session row of "INACTIVE" status, even if I have purposely have the db connection closed in the code. And seems to me, this session will kept for ever. Eventually it kills the pool.
Here is the sample of my code, where "apple" is my connection pool name.
Connection connection = JDBCConnection.getJDBCConnction(apple);
Statement stmt = null;
String query ="select name from user";
String hosts="";
try {
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
name = rs.getString("name");
}
} finally {
connection.close();
}
Is there anything extra I need to do ?
Thanks,
Jack
You are likely running into an issue where you are closing the Connection but it does not result in closing the ResultSet or the Statement.
The topic has been explained extensively here and here on SO.

kerberos auth and connection pooling in jdbc

I've got Java web application running on Tomcat with SSO via SPNEGO/Kerberos and I want to pass kerberos ticket to database, Oracle DB in my case (like impersonation in MS products). I've found an example of implementation (http://docs.oracle.com/cd/B28359_01/java.111/b31224/clntsec.htm):
Connection conn = (Connection)Subject.doAs(specificSubject, new PrivilegedExceptionAction({
public Object run() {
Connection con = null;
Properties prop = new Properties();
prop.setProperty(AnoServices.AUTHENTICATION_PROPERTY_SERVICES,"("+AnoServices.AUTHENTICATION_KERBEROS5 + ")");
try {
OracleDriver driver = new OracleDriver();
con = driver.connect(url, prop);
}catch (Exception except){
except.printStackTrace();
}
return con;
}
});
String auth = ((OracleConnection)conn).getAuthenticationAdaptorName();
System.out.println("Authentication adaptor="+auth);
printUserName(conn);
conn.close();
But as it is known to create a new connection is an expensive operation. To solve this problem commonly used connection pooling (like c3p0), but I cant find example, how to combine code above and connection pool. Is there any example?

I am not able to connect to Oracle XE database in another system?

I have used this code to connect to XE database:
OracleConnection con = new OracleConnection();
con.ConnectionString = "data source=XE;user id=hr;password=sdmo1365";
con.Open();
OracleCommand com = new OracleCommand();
com.CommandText = "select * from regions;";
com.Connection = con;
DataTable dt = new DataTable();
dt.Load(com.ExecuteReader());
con.Close();
grd.DataSource = dt;
grd.DataBind();
it is working in one pc while not working in another and giving this error:
oracleexception was unhandled by user code
the only difference in tnsnames.ora of those pc is in the one that is not working the host is longer(becuase the user is under domain:)
(HOST = MOSININI-9.novini.mms.sti)
is this making the problem?
thanks
I simply reinstalled it and everything get solved!(I waste 2 hours)

Oracle and Transaction naming

I've been programing with SQL Server and C# for quite some time and I have some code that I need to change to work with Oracle:
SqlConnection connection = (SqlConnection)CreateDBConnection();
IDbTransaction transaction = connection.BeginTransaction(level, "name");
The problem is: if I use OracleConnection instead of SqlConnection there is no way to specify a name for my transaction. I know that the syntax in Oracle allows named transactions, but I don't seem to find a way to do it through .net code.
You can not set the transaction name.
But it is possible to specify a name for the SAVEPOINT.
OracleConnection oracleConnection = new OracleConnection((string.Format("Data Source={0};User Id={1};Password={2}", DATABASE, USERNAME, PASSWORD));
oracleConnection.Open();
OracleCommand oracleCommand = oracleConnection.CreateCommand();
oracleConnection.BeginTransaction();
OracleTransaction oracleTransaction = oracleCommand.Transaction;
oracleTransaction.Save("name");
//...
oracleTransaction.Rollback("name");

How to connect to Play Framework in-memory database using JDBC?

I use the in-memory database that comes with Play Framework, when I have db=mem in the configuration file, for development.
How can I connect to this database using JDBC? and not the JPA that is the default way.
I have tried with this method in my controller:
public static void addToDB() {
try {
Connection conn = DriverManager.getConnection("jdbc:h2:mem:play");
Statement stmt = conn.createStatement();
stmt.execute(sql);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
But I get an error message that I need to provide an username and password:
org.h2.jdbc.JdbcSQLException: Wrong user name or password [8004-149]
If I visit the web-console on /#db the username sa is used and no password.
Now I logged in via the /#db interface and created a table users.
Then I connected to the database in a controller method and used this jdbc-string: jdbc:h2:mem:play:sa and then tried to insert to the table users but I get this error message:
org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement:
How should I connect to the in-memory H2 database in Play Framework using JDBC?
DB.getConnection(), should do the job.
Try:
Connection conn = DriverManager.getConnection("jdbc:h2:mem:play", "sa", "");
because as you wrote, "the username sa is used and no password."

Resources