datasource injection - datasource

I need help to understand DataSource injection.
I have defined the jndi datasource in webSphere server and tested the connection within the console return successful.
When I tried to inject the resource inside a servlet
private #Resource(name = "jdbc/order_inquiry") DataSource ds;
ds is null when the code tried to get the connection from ds.
Connection conn = ds.getConnection();
My question is do I need to add anything to the web.xml or other configuration file ?

Related

OpenDJ SDK Thread Pool Exception

We are using OpenDJ SDK for connecting with Directory Services. Below mentioned is code.
#Bean
public LDAPConnectionFactory createConnectionFactory(){
LDAPOptions ldapOptions = new LDAPOptions();
ldapOptions.setTimeout(30, TimeUnit.SECONDS);
final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port,ldapOptions);
Connections.newFixedConnectionPool(factory,connectionPoolSize);
return factory;
}
connection pool size parameter is set to 10 at present. The code was working fine, suddenly it started returning null object for getConnection() method on factory. When I comment out Connections.newFixedConnectionPool statement it works as per expected. Are we missing anything.
If you are creating a fixed connection pool, you should request a connection from it, not from the factory.
The issue is that you are actually not saving the returned pool.

SQL Server 2008 Instance connection issue

My problem is the following:
I installes SQL Server 2008 R2 on my Windows Server 2008. I tried it by using default instance name (MSSQLSERVER) and named instance. The Installation was successful without an error. The problem is now:
When I try to connect to my SQL Server with the Management Studio it can't connect to this instance when I write "SERVER1\MSSQLSERVER" as serveraddress. When I write "SERVER1" only in the serveradress field it works.
Note: I always try to connect as SA. The password is right. Dont know if that matters...
But I have to be able to connect to "SERVER1\MSSQLSERVER" because I always get errors when I want to connect to a server without instance by C#.
Can someone tell me where I am mistaking?
EDIT:
The C# code looks like this:
sqlConnection = "data source=(local);persist security info=True;User ID=sa;Password=12345;initial catalog=BBKat"
SqlConnection sqlCon = new SqlConnection( sqlConnection );
SqlCommand sqlCmd = new SqlCommand( sqlCmdString, sqlCon );
I think you are not mistaken at all. You do not write the default instance name while connecting. You can create aliases though as mentioned above.
Try with a single (local) in the server name. and the same in C# web config connectionString as Data Source=(local);Initial Catalog=YOUR_DB;Integrated Security=True
EDIT
Change the connection string as:
data source=(local);Integrated Security=False;User ID=sa;Password=12345;initial catalog=BBKat

If I use groovy sql class in grails, does it use the grails connection pooling?

From the examples below in the sql documentation. If I use either of these ways to create a sql instance in the middle of a grails service class, will it use the grails connection pooling? Will it participate in any transaction capabilities? Do I need to close the connection myself? Or will it automatically go back into the pool?
def db = [url:'jdbc:hsqldb:mem:testDB', user:'sa', password:'', driver:'org.hsqldb.jdbcDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
or if you have an existing connection (perhaps from a connection pool) or a datasource use one of the constructors:
def sql = new Sql(datasource)
Now you can invoke sql, e.g. to create a table:
sql.execute '''
create table PROJECT (
id integer not null,
name varchar(50),
url varchar(100),
)
'''
If you execute:
Sql.newInstance(...)
You will create a new connection and you aren't using the Connection Pool.
If you want to use the connection pool, you can create a Service with the following command:
grails create-service org.foo.MyService
Then, in your MyService.groovy file, you can manage transactions as follows:
import javax.annotation.PostConstruct
class MyService {
def dataSource // inject the datasource
static transactional = true // tell groovy that the service methods will be transactional
def doSomething() {
sql = new Sql(dataSource)
//rest of your code
}
}
For more details you can read: http://grails.org/doc/2.0.x/guide/services.html
EDIT:
For manage multiple datasources you can do one of the following based on your Grails version.
If you are using a Grails version greater than 1.1.1 (not 2.x) you can use the following plugin:
http://grails.org/plugin/datasources
If you are using Grails 2.x you can use the out of the box support:
http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources
If you create the Sql object like this I believe it will use connection pooling
class SomeSerive {
SessionFactory sessionFactory
def someMethod() {
Sql sql = new Sql(sessionFactory.currentSession.connection())
}
}

Red5: is it alright to store IConnection instances in a HashMap to retrieve later

I have a HashMap defined like this
HashMap<String, IConnection> connections = new HashMap<String, IConnection>();
inside application connect, I add values into it like this:
conn.setAttribute(“username”, username);
connections.put(username, conn); // username and conn are parameters passed to
// connect method
inside application disconnect method, I remove values from it like this
connections.remove((String)conn.getAttribute(“username”));
This seems to work, however is it correct/safe? Or am I doing it wrong?
Yes, it is alright but I suggest that you make sure the connection is still connected before you try to access or write to it.

dataSource injection in a Grails service

I have a service with application scope, not transactional.
I have a service method which:
uses the injected dataSource to create a stored procedure call [using Sql.call{...}]. Executes and traverse the resultset.
Based on the resultset, I subdivide the resultsets into equal sizes chunks and process them in multiple threads.
Each thread tries to do Sql sql = new Sql(dataSource)
Here a deadlock occurs.
Why is that? Does dataSource not return a possibly new or an idle connection?
Try to look into Gpars : It's a groovy parallalization framework.
I run into exactly the same issue. After hours of searching i've found the solution.
In your Datasource.groovy configfile you are able to set the parameters for the connection pooling to the database.
I've changed the minIdle, maxIdle and maxActive settings of the http://commons.apache.org/proper/commons-dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html so that my configfile looks something like this:
dataSource {
url = "jdbc:mysql://127.0.0.1/sipsy_dev?autoReconnect=true&zeroDateTimeBehavior=convertToNull"
driverClassName = "com.mysql.jdbc.Driver"
username = "sipsy_dev"
password = "sipsy_dev"
pooled = true
properties {
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
minIdle=100
maxIdle=250
maxActive=500
validationQuery="SELECT 1"
}
dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
}
When you are not in a transaction, you have to release the connection that GroovySQL picks up from the datasource. The pool runs out of connections and that's why it locks up.
Inside a transaction TransactionAwareDataSourceProxy will take care of sharing the connection and therefore releasing the connection from GroovySQL isn't required in that case. See http://jira.grails.org/browse/GRAILS-5454 for details.
This is a better way to use GroovySQL in Grails since the OpenSessionInView (OSIV) interceptor will take care of closing the connection and it will share the same database connection as Hibernate. This method works in both cases: inside transactions and outside transactions.
Sql sql = new Sql(sessionFactory.currentSession.connection())