I have an application which is configured to connect to a specific database as per the URL with their unique credentials-
eg -
- demo.mydomain.com (DB Name - demo)
- client1.mydomain.com (DB Name - client1)
- client2.mydomain.com (DB Name - client2)
I have defined the data source in context.xml in my web-inf dir as below -
<!-- demo Environment Configuration Starts -->
<Resource type="javax.sql.DataSource"
name="jdbc/demo"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo"
username="demo"
password="demo"
initialSize="10"
maxActive="100"
maxIdle="30"
minIdle="0"
suspectTimeout="60"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"
/>
<!-- Client1 Environment Configuration Starts -->
<Resource type="javax.sql.DataSource"
name="jdbc/client1"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/client1"
username="client1"
password="client1"
initialSize="10"
maxActive="100"
maxIdle="30"
minIdle="0"
suspectTimeout="60"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"
/>
The list goes on for all the available clients.
The SysServlet is defined in web.xml with a URL mapping -
<servlet>
<servlet-name>SysInfo</servlet-name>
<servlet-class>com.emsproject.action.common.SysServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SysInfo</servlet-name>
<url-pattern>/eapp/*</url-pattern>
</servlet-mapping>
public class SysServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet{
static final long serialVersionUID = 1L;
private static DataSource dataSource;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try {
String clientName = request.getPathInfo();
String esmPool = clientName.substring(1, clientName.length()).trim();
/******************************************************
* Below code prepares the datasource by JNDI lookup *
******************************************************/
dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/"+esmPool);
} catch (Exception e) {
e.printStackTrace();
}
}
/*Returns a connection */
public Connection getConnection() throws Exception {
return dataSource.getConnection();
}
/*Closing connection when transaction is over*/
public void freeConnection(Connection dbConn) {
try {
if(null != dbConn){
dbConn.close();
}
} catch (SQLException e){
e.printStackTrace();
}
}
}
The Application is built using -
- Struts 2
- MySql 5
- Tomcat 6
The application works fine without any issue for a single client at a time but the problems I am facing are as below -
When a client1 performs some insert/update it is reflected to client2 & so on when application is accessed simultaneously by them.
When a Client1 performs a login (I keep certain details in session) & later Client2 logs in to the system the Client2 info is shared or visible to client1.
Simultaneous access is troubling me Kindly suggest me a solution for the same ....
I have made few changes to the code as below while fetching a connection -
public Connection getConnection(String clientName) throws Exception {
DataSource dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/"+clientName);
return dataSource.getConnection();
}
But I have one concern regarding the statement -
DataSource dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/"+clientName);
Is it ok to initialize a DataSource every time I need a connection...
Kindly suggest...